我有一個JavaScript嵌套的對象,如下所示:提取和合並數組
input_data = {
"A1":{
"B1":{
"M1" : [
{"E1":"10","E2":"11"},
{"E1":"20","E2":"21"}
],
"M2":[
{"E1":"30","E2":"31"},
{"E1":"40","E2":"41"}
],
"M3":[
{"E1":"50","E2":"51"}
]
}
},
"A2":{
"B2":{
"M1": [
{"E1":"60","E2":"61"},
{"E1":"70","E2":"71"}
],
"M2":[
{"E1":"80","E2":"81"},
{"E1":"90","E2":"91"}
]
}
}
}
我需要提取下「M1」,「M2」,「M3」的所有項目成陣列,即,輸出應,如下所示:
output_data = [
{"E1":"10","E2":"11"},
{"E1":"20","E2":"21"},
{"E1":"30","E2":"31"},
{"E1":"40","E2":"41"},
{"E1":"50","E2":"51"},
{"E1":"60","E2":"61"},
{"E1":"70","E2":"71"},
{"E1":"80","E2":"81"},
{"E1":"90","E2":"91"}
];
我可以以下面的方式實現這一點:
var output_data = [];
function traverse(obj) {
for (i in obj) {
if (!!obj[i] && typeof(obj[i])=="object") {
if (Array.isArray(obj[i])){
output_data = output_data.concat([], obj[i]);
}
traverse(obj[i]);
}
}
}
traverse(input_data);
console.log(output_data);
爲Th創建一個更好的方法來提取子數組項併合併成一個?
如果它運行你那麼這屬於在代碼審查。在這種情況下,我會說在函數內移動output_data數組,並在for循環後面的函數底部返回它 –