Javascript JS(ES6):基于对象属性的约简数组
我有这样一个数组:Javascript JS(ES6):基于对象属性的约简数组,javascript,arrays,ecmascript-6,reduce,Javascript,Arrays,Ecmascript 6,Reduce,我有这样一个数组: const fruits = [ { fruit: 'apple', year: 2018 }, { fruit: 'apple', year: 2018 }, { fruit: 'banana', year: 2018 }, { fruit: 'orange', year: 2018 }, { fruit: 'apple', year: 2017 }, { fruit: 'apple', year: 2016 } ]
const fruits = [
{ fruit: 'apple', year: 2018 },
{ fruit: 'apple', year: 2018 },
{ fruit: 'banana', year: 2018 },
{ fruit: 'orange', year: 2018 },
{ fruit: 'apple', year: 2017 },
{ fruit: 'apple', year: 2016 }
];
const result = [
{ year: 2018, apple: 2, banana: 1, orange: 1 total: 4 },
{ year: 2017, apple: 1, total: 1 },
{ year: 2016, apple: 1, total: 1 }
];
现在我想减少这个数组,看看每年有多少水果,总共有多少。结果如下所示:
const fruits = [
{ fruit: 'apple', year: 2018 },
{ fruit: 'apple', year: 2018 },
{ fruit: 'banana', year: 2018 },
{ fruit: 'orange', year: 2018 },
{ fruit: 'apple', year: 2017 },
{ fruit: 'apple', year: 2016 }
];
const result = [
{ year: 2018, apple: 2, banana: 1, orange: 1 total: 4 },
{ year: 2017, apple: 1, total: 1 },
{ year: 2016, apple: 1, total: 1 }
];
我尝试过使用reduce,但我不知道如何根据年份对其进行分组。可能还有lodash助手?使用
对象。值和减少
var output = Object.values(fruits.reduce( (a,c) => {
a[c.year] = a[c.year] || { year : c.year };
a[c.year]["total"] = (a[c.year]["total"] || 0) + 1;
a[c.year][c.fruit] = (a[c.year][c.fruit] || 0) + 1;
return a;
},{}));
演示
var=[{
水果:“苹果”,
年份:2018年
},
{
水果:“苹果”,
年份:2018年
},
{
水果:“香蕉”,
年份:2018年
},
{
水果:“橙色”,
年份:2018年
},
{
水果:“苹果”,
年份:2017年
},
{
水果:“苹果”,
年份:2016年
}
];
var输出=对象.值(水果.减少((a,c)=>{
一年{
年份:c.year
};
a[c.年][“总计”]=(a[c.年][“总计”]| | 0)+1;
a[c.年][c.果]=(a[c.年][c.果]| | 0)+1;
返回a;
}, {}));
控制台日志(输出)代码>使用对象。值
和减少
var output = Object.values(fruits.reduce( (a,c) => {
a[c.year] = a[c.year] || { year : c.year };
a[c.year]["total"] = (a[c.year]["total"] || 0) + 1;
a[c.year][c.fruit] = (a[c.year][c.fruit] || 0) + 1;
return a;
},{}));
演示
var=[{
水果:“苹果”,
年份:2018年
},
{
水果:“苹果”,
年份:2018年
},
{
水果:“香蕉”,
年份:2018年
},
{
水果:“橙色”,
年份:2018年
},
{
水果:“苹果”,
年份:2017年
},
{
水果:“苹果”,
年份:2016年
}
];
var输出=对象.值(水果.减少((a,c)=>{
一年{
年份:c.year
};
a[c.年][“总计”]=(a[c.年][“总计”]| | 0)+1;
a[c.年][c.果]=(a[c.年][c.果]| | 0)+1;
返回a;
}, {}));
控制台日志(输出)
您可以使用array#reduce
根据年份对数据进行分组,并计算对象累加器中水果的出现次数。然后使用object.values()
const fruits=[{水果:苹果,年份:2018},{水果:苹果,年份:2018},{水果:香蕉,年份:2018},{水果:橙色,年份:2018},{水果:苹果,年份:2017},{水果:苹果,年份:2016},
result=Object.values(fruits.reduce((r,{fruit,year})=>{
r[年]=r[年]|{年};
r[年][果]=(r[年][果]| 0)+1;
r[年]['total']=(r[年]['total']||0)+1;
返回r;
},{}));
控制台日志(结果)
您可以使用array#reduce
根据年份对数据进行分组,并计算对象累加器中水果的出现次数。然后使用object.values()
const fruits=[{水果:苹果,年份:2018},{水果:苹果,年份:2018},{水果:香蕉,年份:2018},{水果:橙色,年份:2018},{水果:苹果,年份:2017},{水果:苹果,年份:2016},
result=Object.values(fruits.reduce((r,{fruit,year})=>{
r[年]=r[年]|{年};
r[年][果]=(r[年][果]| 0)+1;
r[年]['total']=(r[年]['total']||0)+1;
返回r;
},{}));
控制台日志(结果)代码>您可以使用一个数组作为结果集,它保持一年中给定的顺序
var fruits=[{水果:苹果,年份:2018},{水果:苹果,年份:2018},{水果:香蕉,年份:2018},{水果:橙色,年份:2018},{水果:苹果,年份:2017},{水果:苹果,年份:2016}],
结果=果实。减少((r,{年份,果实})=>{
var项目=r.find(o=>o.year==year);
如果(!项){
r、 推送(项目={year});
}
物品[水果]=(物品[水果]| | 0)+1;
返回r;
}, []);
控制台日志(结果)代码>
.as console wrapper{max height:100%!important;top:0;}
您可以使用数组作为结果集,它保持给定的年度顺序
var fruits=[{水果:苹果,年份:2018},{水果:苹果,年份:2018},{水果:香蕉,年份:2018},{水果:橙色,年份:2018},{水果:苹果,年份:2017},{水果:苹果,年份:2016}],
结果=果实。减少((r,{年份,果实})=>{
var项目=r.find(o=>o.year==year);
如果(!项){
r、 推送(项目={year});
}
物品[水果]=(物品[水果]| | 0)+1;
返回r;
}, []);
控制台日志(结果)代码>
。作为控制台包装{最大高度:100%!重要;顶部:0;}