要累加对象数组中包含的值,必须提供初始值,以便各个item正确通过你的函数。
var initialValue=0;
var sum=[{x:1},{x:2},{x:3}].reduce(function(accumulator,currentValue){returnaccumulator+currentValue.x;},initialValue)
console.log(sum)// logs 6
要累加对象数组中包含的值,必须提供初始值,以便各个item正确通过你的函数。
var initialValue=0;
var sum=[{x:1},{x:2},{x:3}].reduce(function(accumulator,currentValue){returnaccumulator+currentValue.x;},initialValue)
console.log(sum)// logs 6
本文标题:js对象数组,怎么求和对象的某个属性呢
本文链接:https://www.haomeiwen.com/subject/kzrtwctx.html
网友评论