假設我有以下的JSON文件,我想與lodash preferrably返回「existing.primaryBundle.products」數組:如何使用lodash從json中返回一個childobject?
{
"existing": {
"hasPlatinum": false,
"primaryBundle": {
"id": "2008",
"name": "TV - Entertainment, Sport",
"products": [
{
"name": "Entertainment",
"id": "100",
"price": 2600,
"gifted": false
},
{
"name": "Sport",
"id": "107",
"price": 2500,
"gifted": false,
"swappableProducts": [
{
"name": "Movies",
"id": "105",
"price": 2000,
"gifted": false
}
]
}
]
},
"extrasBundle": {
"id": "131",
"name": "Optional Extras - MUTV (Sports), LFCTV (Sports), Chelsea TV (Sports)",
"products": [
{
"name": "MUTV (Sports)",
"id": "665",
"price": 0,
"gifted": false
},
{
"name": "LFCTV (Sports)",
"id": "666",
"price": 0,
"gifted": false
},
{
"name": "Chelsea TV (Sports)",
"id": "667",
"price": 0,
"gifted": false
}
]
}
}
}
我曾嘗試lodash並使用此聲明:
列表2 = _.pick(現有,'primaryBundle.products')
但是,這會返回一個錯誤,而不是想要的結果。我怎樣才能選擇這個產品陣列?
假設您已經解析的JSON字符串* *獲取對象'obj',你可以說'列表2 = obj.existing.primaryBundle.products'。不需要Lodash。 – nnnnnn