我有数据
season
和
fielding
数组。
实战化
数组有3个参数
Catches
,请
Runouts
,请
Stumpings
.我想计算总数
捕捉
,请
树桩
,请
耗尽
.
var data = [
{
season:2015,
fielding:{Catches:2, Runouts:1, Stumpings:1
},
{
season:2016,
fielding:{Catches:0, Runouts:1, Stumpings:1
},
{
season:2016,
fielding:{Catches:0, Runouts:0, Stumpings:0
},
{
season:2017,
fielding:{Catches:1, Runouts:3, Stumpings:1
},
{
season:2017,
fielding:{Catches:2, Runouts:1, Stumpings:2
}
]
我希望最终输出为以下对象:
Catches -> 2+0+0+1+2 = 5
Runouts -> 1+1+0+3+1 = 6
Stumpings -> 1+1+0+1+2 =5
我的尝试:
let dismissals = data.reduce( (a,{season, fielding}) => {
if(!a[fielding.Catches]){
a[fielding.Catches] = {};
}else if(a[fielding.Runouts]){
a[fielding.Runouts] = {};
}else if(a[fielding.Stumpings]){
a[fielding.Stumpings] = {};
}else{
a[fielding.Stumpings] += 1;
a[fielding.Runouts] += 1;
a[fielding.Catches] += 1;
}
return a;
}, {});
我的代码不计算所需的输出并给出
NaN
.