| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

问题点数：0 回复次数：3

[
[ "a", 30.0, 27.969999999999999, 0.0 ],
[ "a", 20.0, 20.66, 0.0 ],
[ "b", 160.0, 127.08, 5.0 ],
[ "b", 30.0, 2.0, 1.0 ],
[ "c", 1.0, 0.0, 0.0 ],
[ "a", 10.0, 10.859999999999999, 0.0 ],
[ "b", 8.0, 5.3200000000000003, 1.0 ],
[ "b", 20.0, 9.6500000000000004, 0.0 ],
[ "a", 20.0, 11.300000000000001, 1.0 ],
[ "d", 60.0, 56.609999999999999, 6.0 ],
[ "d", 60.0, 57.340000000000003, 4.0 ],
[ "c", 90.0, 72.420000000000002, 8.0 ],
[ "a", 80.0, 7.9000000000000004, 1.0 ]
]

[
[ "a", 160.0, 78.689999999999998, 2.0 ],
[ "b", 218.0, 144.05000000000001, 7.0 ],
[ "c", 91.0, 72.420000000000002, 8.0 ],
[ "d", 120.0, 113.95, 10.0 ]
]

```function accumulation(arrs = []){
var result = arrs.reduce((counter=[],arr)=>{
let index, key = arr[0]
let elem = counter && counter.find((i,_index)=>{
return i[0]===key ? index = _index : false
})

if(elem) {
counter[index] = [key, elem[1]+arr[1], arr[2]+arr[2], elem[3]+arr[3] ]
} else {
counter.push(arr)
}
return counter
})
return result.splice(arr.length)
}
accumulation([
[ "a", 30.0, 27.969999999999999, 0.0 ],
[ "a", 20.0, 20.66, 0.0 ],
[ "b", 160.0, 127.08, 5.0 ],
[ "b", 30.0, 2.0, 1.0 ],
[ "c", 1.0, 0.0, 0.0 ],
[ "a", 10.0, 10.859999999999999, 0.0 ],
[ "b", 8.0, 5.3200000000000003, 1.0 ],
[ "b", 20.0, 9.6500000000000004, 0.0 ],
[ "a", 20.0, 11.300000000000001, 1.0 ],
[ "d", 60.0, 56.609999999999999, 6.0 ],
[ "d", 60.0, 57.340000000000003, 4.0 ],
[ "c", 90.0, 72.420000000000002, 8.0 ],
[ "a", 80.0, 7.9000000000000004, 1.0 ]
])
/*
result => [
["a", 130, 15.8, 2],
["b", 218, 19.3, 7],
["c", 91, 144.84, 8],
["d", 120, 114.68, 10]
]
*/
```

`return result.splice(arr.length)`

`return result.splice(4)`
• 4
• 1/1页
• 1