reactjs以对象数组

时间:2019-03-14 17:38:17

标签: reactjs

[Codepen Reactjs] [1]

`https://codepen.io/sushil6263/pen/NJXQKo`

如何以对象数组的形式计算状态中存在的所有值的总和。并且当我们删除一个项目的总价值时,需要进行更改

1 个答案:

答案 0 :(得分:0)

希望这有帮助

var accounts = [
      { name: 'James Brown', msgCount: 123 },
      { name: 'Stevie Wonder', msgCount: 22 },
      { name: 'Sly Stone', msgCount: 16 },
      { name: 'Otis Redding', msgCount: 300 }
    ];

// get sum of msgCount prop across all objects in array
var msgTotal = accounts.reduce((acc, cur) => (acc + cur.msgCount), 0);