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

时间:2018-05-03 15:33:43

标签: javascript arrays 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
        },
    }
};

基本上我想要一个像这样的数组,只有值:

{
    "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谢谢你!

3 个答案:

答案 0 :(得分:7)

这种递归方法使用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);




答案 1 :(得分:2)

这是工作代码



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




答案 2 :(得分:1)

通过递归函数跟踪对象:



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