1

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
0

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

const result = {"reports": getObjectValues(obj)}; will get the result in desired format
2

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);

answered May 3, 2018 at 15:41

Comments

1

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

2 Comments

You're missing the null values because typeof null === "object"; // true
@CrazyTrain Thank you, you are right, I have fixed it in my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.