[英]Extract values from nested JSON to array using JS
I have a JSON file with nested objects, I need to extract a specific key's value from each object and save to an array.我有一个带有嵌套对象的 JSON 文件,我需要从每个 object 中提取特定键的值并保存到数组中。 No need to preserve the structure or order.
无需保留结构或顺序。
Please see JSON below.请参阅下面的 JSON。 I need to extract the values of the
'text'
key and have a resulting array like this ["CPUs", "AMD", "Ryzen", "intel",....]
我需要提取
'text'
键的值并得到一个像这样的数组["CPUs", "AMD", "Ryzen", "intel",....]
What is the best way to achieve this?实现这一目标的最佳方法是什么?
[
{
"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"
}
]
Simple recursion should help to iterate through the array to get the objects with the text
property简单的递归应该有助于遍历数组以获取具有
text
属性的对象
const data = [{"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"}]; function getText(item) { let result = []; if (item.text) result.push(item.text); for (const key in item) { if (item.hasOwnProperty(key) && typeof item == 'object') result.push(...getText(item[key])); } return result; } data.forEach(item => console.log(getText(item)));
You can write a recursive function which returns the text
value at the current node, then all the text
values from its child items
:您可以编写一个递归 function ,它返回当前节点处的
text
值,然后是其子items
中的所有text
值:
const data = [{"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"}]; const extract = (arr) => arr.reduce((acc, obj) => acc.concat(obj.text, extract(obj.items || [])), []) out = extract(data); console.log(out);
Here is an iterative solution using object-scan .这是使用object-scan的迭代解决方案。 To me it's more readable and maintainable than writing a custom recursive function.
对我来说,它比编写自定义递归 function 更具可读性和可维护性。 But adding a dependency is obviously a tradeoff
但是添加依赖显然是一种权衡
// const objectScan = require('object-scan'); const data = [{ 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' }]; const find = objectScan(['**(^items$).text'], { useArraySelector: false, rtn: 'value' }); console.log(find(data)); /* => [ 'cars', 'sports cars', 'ferrari', 'maserati', 'porsche', 'computer parts', 'CPUs', 'Apple', 'Intel', 'i3', 'i7', 'i5', 'AMD', 'Threadripper', 'Ryzen' ] */
.as-console-wrapper {max-height: 100%;important: top: 0}
<script src="https://bundle.run/object-scan@14.3.0"></script>
Disclaimer : I'm the author of object-scan免责声明:我是对象扫描的作者
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.