简体   繁体   English

获取嵌套对象/数组JavaScript的值

[英]get the values of nested object/array JavaScript

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! 我有这个repl.it如果它可以帮助https://repl.it/@lizparody/UnlinedCruelResearch谢谢!

This recursive method, uses Object.values() to get the current object's values. 此递归方法使用Object.values()获取当前对象的值。 Values are iterated with Array.reduce() . 值使用Array.reduce()进行迭代。 If the value is an object (and not null ), it's iterated with the method as well. 如果该值是一个对象(而不是null ),则也将使用方法进行迭代。 The actual values are combined to a single array with Array.concat() : 实际值通过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); 

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM