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!
asked May 3, 2018 at 15:33
Lizz Parody
1,76513 gold badges32 silver badges50 bronze badges
3 Answers 3
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);
answered May 3, 2018 at 15:40
Ori Drori
196k32 gold badges243 silver badges233 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Md Johirul Islam
const result = {"reports": getObjectValues(obj)}; will get the result in desired formatHere 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);
answered May 3, 2018 at 15:41
Md Johirul Islam
5,1924 gold badges28 silver badges57 bronze badges
Comments
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);
answered May 3, 2018 at 15:52
Alexey Usachov
1,3742 gold badges9 silver badges15 bronze badges
2 Comments
Alexey Usachov
@CrazyTrain Thank you, you are right, I have fixed it in my answer.
lang-js