[英]Extract values from nested JSON to array using JS
我有一個帶有嵌套對象的 JSON 文件,我需要從每個 object 中提取特定鍵的值並保存到數組中。 無需保留結構或順序。
請參閱下面的 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"
}
]
簡單的遞歸應該有助於遍歷數組以獲取具有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)));
您可以編寫一個遞歸 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);
這是使用object-scan的迭代解決方案。 對我來說,它比編寫自定義遞歸 function 更具可讀性和可維護性。 但是添加依賴顯然是一種權衡
// 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>
免責聲明:我是對象掃描的作者
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.