如何获得所有属性的总和?

时间:2018-07-28 03:53:11

标签: node.js

给出:

{
  "property1": 10,
  "property2": 20,
  "property3": 30
}

我想添加所有属性的总属性,如下所示:

{
  "property1": 10,
  "property2": 20,
  "property3": 30,
  "Total": 60
}

1 个答案:

答案 0 :(得分:3)

您可以使用array#reduceObject.values()

let o = { "property1": 10, "property2": 20, "property3": 30 };
o.Total = Object.values(o).reduce((s,v) => s + +v, 0);
console.log(o);