【发布时间】:2020-02-23 15:27:33
【问题描述】:
给定一个这样的对象数组:
[
{
playerId: 1,
playerName: "Cooper",
assists: 1,
points: 5,
},
{
playerId: 1,
playerName: "Cooper",
assists: 3,
points: 2,
},
{
playerId: 2,
playerName: "Tom",
assists: 1,
points: 3,
},
{
playerId: 2,
playerName: "Tom",
assists: 3,
points: 1,
},
{
playerId: 3,
playerName: "Shelby",
assists: 2,
points: 7,
},
{
playerId: 3,
playerName: "Shelby",
assists: 1,
points: 2,
},
]
有没有办法使用 ES6 在对象数组中找到匹配的对象键(本例中为 playerId),然后为这些匹配组合其他对象键(助攻和点),因此最终数组看起来像这样:
[
{
playerId: 1,
playerName: "Cooper",
assists: 4,
points: 7,
},
{
playerId: 2,
playerName: "Tom",
assists: 4,
points: 4,
},
{
playerId: 3,
playerName: "Shelby",
assists: 3,
points: 9,
},
]
【问题讨论】:
标签: javascript arrays