给出:
{
"property1": 10,
"property2": 20,
"property3": 30
}
我想添加所有属性的总属性,如下所示:
{
"property1": 10,
"property2": 20,
"property3": 30,
"Total": 60
}
答案 0 :(得分:3)
您可以使用array#reduce
和Object.values()
。
let o = { "property1": 10, "property2": 20, "property3": 30 };
o.Total = Object.values(o).reduce((s,v) => s + +v, 0);
console.log(o);