gpt4 book ai didi

javascript - 在 NodeJS 中访问 JSON 中的值

转载 作者:行者123 更新时间:2023-12-02 22:10:29 26 4
gpt4 key购买 nike

我有一个函数,它将接受一个 JSON 对象,其结构如下:-

{
"listing": [
{
"rental_prices": {
"shared_occupancy": "N",
"per_week": 2308,
"accurate": "per_month",
"per_month": 10000
},
"country_code": "gb",
"num_floors": "0",
}
]
}

我希望能够获取 per_month 的所有值来自 listing 的每个子对象我可以提取一个特定的,即使用 data.listing[0].rental_prices.per_month但我希望能够迭代该对象并获取值,并在每次它跨越 per_month 时将其推送到一个新数组。关键

最佳答案

确实,正如 @mwilson 所建议的那样,在这种情况下 .map 可以解决问题。

https://repl.it/repls/TriangularModestNormalform

testJson.listing.map(listing => listing.rental_prices.per_month);

注意:如果 per_month 不存在,这将返回一个包含未定义项目的数组,如果 Renal_prices 不存在,也会崩溃

关于javascript - 在 NodeJS 中访问 JSON 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59572256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com