[英]How to get deeply nested object structure from array of values with Javascript
[英]get the values of nested object/array JavaScript
如何獲取此嵌套對象的數組中的所有值:
{
"report": {
"firstSection": {
"totalIncome": 9650000,
"category": null,
"mustPay": null,
"tax": null,
"bef": null,
"message": "Los ingresos exceden el monto máximo para la modalidad monotributo"
},
"secondSection": {
"subTotals": {
"intTotal": 6295.166666666666,
"ordTotal": 3884679.201041667
},
"unitaryProductionCost": 247.55291005291008,
"unitaryInfo": {
"unitarySalesCost": 16338.425925925927,
"unitarySalesPrice": 23536.585365853658
},
"bankDebts": 0,
"monthlySimpleDepreciation": 173333.33333333334
},
}
};
基本上,我想要這樣的數組,只包含值:
{
"report": [
9650000,
null,
null,
null,
null,
"Los ingresos exceden el monto máximo para la modalidad monotributo",
6295.166666666666,
3884679.201041667,
247.55291005291008,
16338.425925925927,
23536.585365853658,
0,
173333.33333333334,
]
}
我有這個repl.it如果它可以幫助https://repl.it/@lizparody/UnlinedCruelResearch謝謝!
此遞歸方法使用Object.values()
獲取當前對象的值。 值使用Array.reduce()
進行迭代。 如果該值是一個對象(而不是null
),則也將使用方法進行迭代。 實際值通過Array.concat()
組合到單個數組中:
const obj = {"report":{"firstSection":{"totalIncome":9650000,"category":null,"mustPay":null,"tax":null,"bef":null,"message":"Los ingresos exceden el monto máximo para la modalidad monotributo"},"secondSection":{"subTotals":{"intTotal":6295.166666666666,"ordTotal":3884679.201041667},"unitaryProductionCost":247.55291005291008,"unitaryInfo":{"unitarySalesCost":16338.425925925927,"unitarySalesPrice":23536.585365853658},"bankDebts":0,"monthlySimpleDepreciation":173333.33333333334}}}; const getObjectValues = (obj) => Object.values(obj).reduce((r, v) => r.concat(v && typeof v === 'object' ? getObjectValues(v) : v) , []); const result = getObjectValues(obj); console.log(result);
這是工作代碼
var data = { "report": { "firstSection": { "totalIncome": 9650000, "category": null, "mustPay": null, "tax": null, "bef": null, "message": "Los ingresos exceden el monto máximo para la modalidad monotributo" }, "secondSection": { "subTotals": { "intTotal": 6295.166666666666, "ordTotal": 3884679.201041667 }, "unitaryProductionCost": 247.55291005291008, "unitaryInfo": { "unitarySalesCost": 16338.425925925927, "unitarySalesPrice": 23536.585365853658 }, "bankDebts": 0, "monthlySimpleDepreciation": 173333.33333333334 }, } }; var ret = {"reports":[]} function getleafs(obj) { for (var key in obj) { if (obj[key] && typeof obj[key] === "object") { getleafs(obj[key]); } else { ret["reports"].push(obj[key]); } } } getleafs(data); console.log(ret);
通過遞歸函數跟蹤對象:
var obj = { "report": { "firstSection": { "totalIncome": 9650000, "category": null, "mustPay": null, "tax": null, "bef": null, "message": "Los ingresos exceden el monto máximo para la modalidad monotributo" }, "secondSection": { "subTotals": { "intTotal": 6295.166666666666, "ordTotal": 3884679.201041667 }, "unitaryProductionCost": 247.55291005291008, "unitaryInfo": { "unitarySalesCost": 16338.425925925927, "unitarySalesPrice": 23536.585365853658 }, "bankDebts": 0, "monthlySimpleDepreciation": 173333.33333333334 }, } }; function tracer(obj, arr) { if ( typeof obj === 'object' ) { for( key in obj) { if ( obj[key] == null ) { arr.push(obj[key]); } else if ( typeof obj[key] === 'object' ) { arr = tracer(obj[key],arr); } else { arr.push(obj[key]); } } } return arr; } var report = {report:[]}; report["report"] = tracer(obj, []); console.log(report);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.