发布于 5年前

怎么让js对象中的对象的值相加

数字对象

var data= {
        "67407685": {
                "24888861": 200,  
                "23675650": 300   
        },
        "24888861": {
                "23675650": 789,
                "23127112": 400
        }, 
        "23675650": {
                "24888861": 789,
                "67407685": 900
        },
        "23127112": {
                "5388329": 340,
                "24888861": 123
        },
        "38121665": {
                "24888861": 400,
                "23675650": 123
        },
        "5388329": {
                "24888861": 789,
                "23127112": 400
        }
}

方法

let count = 0;
for (let [, value] of Object.entries(data)) {
        for ([k, v] of Object.entries(value)) {
                count += v;
        }
}
console.log(count);
©2020 edoou.com   京ICP备16001874号-3