[英]how to iterate over nested array object in javascript
我想知道如何遍歷Javascipt中嵌套的對象數組嗎? 我有一個名為obj的示例對象。 我想基於條件“輸入”為“字符串”和“輸出”為“數字”來檢索對象。
// tried got stuck
const output = [];
myList.forEach(entry => {
Object.keys(entry).forEach(key => {
const entity = entry[key][0];
if (entity.in === "string" && entity.out === "number") {
output.push(entity);
}
});
});
var obj = [{
"ston": [{
"id": "identity1",
"in": "string",
"out": "number",
"value": 10
},{
"id": "identity2",
"in": "string",
"out": "number",
"value": 10
}],
"nton": [{
"id": "identity1",
"in": "number",
"out": "number",
"value": 20
},{
"id": "identity2",
"in": "number",
"out": "number",
"value": 30
}]
}]
預期產量
[{
"id": "identity1",
"in": "string",
"out": "number",
"value": 10
},{
"id": "identity2",
"in": "string",
"out": "number",
"value": 10
}]
簡單的遞歸函數:
var obj = [{ "ston": [{ "id": "identity1", "in": "string", "out": "number", "value": 10 }, { "id": "identity2", "in": "string", "out": "number", "value": 10 }], "nton": [{ "id": "identity1", "in": "number", "out": "number", "value": 20 }, { "id": "identity2", "in": "number", "out": "number", "value": 30 }] }]; function getObjects(inVal, outVal) { var matches = []; obj.forEach(child => { Object.values(child).forEach(arr => { if (arr.some(e => e.in == inVal && e.out == outVal)) { matches.push([...arr.filter(e => e => e.in == inVal && e.out == outVal)]); } }); }); return matches.flat(); } console.log(getObjects("string", "number"));
在這里,您有一個解決方案,該解決方案主要使用Array.reduce()遍歷數組的外部對象,從每個外部對象獲取值的展平數組,以創建具有內部對象的數組,然后過濾滿足條件的對象,同時保存他們在一個新的數組:
var obj = [ { "ston": [ {"id": "identity1", "in": "string", "out": "number", "value": 10}, {"id": "identity2", "in": "string", "out": "number", "value": 10} ], "nton": [ {"id": "identity1", "in": "number", "out": "number", "value": 20}, {"id": "identity2", "in": "number", "out": "number", "value": 30} ] } ]; let res = obj.reduce((acc, o) => { acc = acc.concat(Object.values(o).flat().filter( o => o.in === "string" && o.out === "number" )); return acc; }, []); console.log(res);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
其他有關使用方法的文檔:
或者,經過一段時間的思考,您可以將下一個簡化版本與Array.map()一起使用
var obj = [ { "ston": [ {"id": "identity1", "in": "string", "out": "number", "value": 10}, {"id": "identity2", "in": "string", "out": "number", "value": 10} ], "nton": [ {"id": "identity1", "in": "number", "out": "number", "value": 20}, {"id": "identity2", "in": "number", "out": "number", "value": 30} ] } ]; let res = obj.map(Object.values).flat(2).filter( o => o.in === "string" && o.out === "number" ); console.log(res);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
您可以使用將該對象重建為嵌套數組,然后展平並最終進行filter
。
var obj = [ { "ston": [ {"id": "identity1", "in": "string", "out": "number", "value": 10}, {"id": "identity2", "in": "string", "out": "number", "value": 10} ], "nton": [ {"id": "identity1", "in": "number", "out": "number", "value": 20}, {"id": "identity2", "in": "number", "out": "number", "value": 30} ] } ]; const tmp = obj.map(e => Object.entries(e).map(([k, v]) => v)).flat(3) const rs = tmp.filter(e => e.out==='number' && e.in ==='string') console.log(rs)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.