【发布时间】:2021-05-05 07:59:27
【问题描述】:
我有一个包含嵌套对象的 JSON 文件,我需要从每个对象中提取特定键的值并保存到数组中。无需保留结构或顺序。
请参阅下面的 JSON。我需要提取 'text' 键的值并得到一个像这样的结果数组
["CPUs", "AMD", "Ryzen", "intel",....]
实现这一目标的最佳方法是什么?
[
{
"itemID":"1",
"items":[
{
"itemID":"2",
"items":[
{
"itemID":"3",
"items":[
{
"itemID":"15",
"text":"Ryzen"
},
{
"itemID":"16",
"text":"Threadripper"
}
],
"text":"AMD",
},
{
"itemID":"66",
"items":[
{
"itemID":"76",
"text":"i5"
},
{
"itemID":"77",
"text":"i7"
},
{
"itemID":"78",
"text":"i3"
}
],
"text":"Intel"
},
{
"itemID":"70",
"text":"Apple"
}
],
"text":"CPUs"
}
],
"text":"computer parts"
},
{
"itemID":"4",
"items":[
{
"itemID":"5",
"items":[
{
"itemID":"21",
"text":"porsche"
},
{
"itemID":"22",
"text":"maserati"
},
{
"itemID":"23",
"text":"ferrari"
}
],
"text":"sports cars"
}
],
"text":"cars"
}
]
【问题讨论】:
标签: javascript json javascript-objects