Reputation: 1765
How can I get all the values in an array of this nested object:
{
"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
},
}
};
Basically I want an array like this, only with the values:
{
"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,
]
}
I have this repl.it if it helps https://repl.it/@lizparody/UnlinedCruelResearch Thank you!
Upvotes: 1
Views: 89
Reputation: 5162
Here is the working code
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);
Upvotes: 2
Reputation: 1374
Trace object by recursive function:
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);
Upvotes: 1
Reputation: 191976
This recursive method, uses Object.values()
to get the current object's values. Values are iterated with Array.reduce()
. If the value is an object (and not null
), it's iterated with the method as well. The actual values are combined to a single array with 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);
Upvotes: 7