ES6数组中按对象属性求和 [英] ES6 Sum by object property in an array
本文介绍了ES6数组中按对象属性求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图按日期对单位值求和,并创建一个没有重复日期的新数组.例如,我要计算 2015-12-04 00:01:00
的总数.此日期在以下数据中发生了2次,其值为 5
和 6
,该值将为:
I am trying to sum the unit value by date, and create a new array where there are no duplicate dates. For example, I want to calculate the total of 2015-12-04 00:01:00
. This date has occurred 2 times in the following data, its value is 5
and 6
, which is going to be:
[{date: '2015-12-04 00:01:00', unit: 11}, ... etc]
我尝试了 arr = results.map(x => x.unit).reduce((a,c)=> a + c)
,但它只返回一个值,不是数组.
I have tried arr = results.map(x => x.unit).reduce((a,c) => a + c)
but it only return a single value, not an array.
results = [ { unit: 5, date: '2015-12-04 00:01:00' },
{ unit: 10, date: '2015-12-04 00:01:00' },
{ unit: 5, date: '2015-12-04 00:31:00' },
{ unit: 9, date: '2015-12-04 00:31:00' },
{ unit: 5, date: '2015-12-04 01:01:00' },
{ unit: 10, date: '2015-12-04 01:01:00' },
{ unit: 10, date: '2015-12-04 01:31:00' },
{ unit: 5, date: '2015-12-04 01:31:00' },
{ unit: 10, date: '2015-12-04 02:01:00' },
{ unit: 5, date: '2015-12-04 02:01:00' },
{ unit: 5, date: '2015-12-04 02:31:00' },
{ unit: 9, date: '2015-12-04 02:31:00' },
{ unit: 5, date: '2015-12-04 03:01:00' },
{ unit: 9, date: '2015-12-04 03:01:00' },
{ unit: 5, date: '2015-12-04 03:31:00' },
{ unit: 10, date: '2015-12-04 03:31:00' },
{ unit: 10, date: '2015-12-04 04:01:00' },
{ unit: 5, date: '2015-12-04 04:01:00' }];
arr = results.map(x => x.unit).reduce((a,c) => a + c);
console.log(arr);
推荐答案
您可以将数据精简为字典,然后将其映射到数组,像这样
You can reduce your data to a dictionary and then map it to an array like so
results = [ { unit: 5, date: '2015-12-04 00:01:00' },
{ unit: 10, date: '2015-12-04 00:01:00' },
{ unit: 5, date: '2015-12-04 00:31:00' },
{ unit: 9, date: '2015-12-04 00:31:00' },
{ unit: 5, date: '2015-12-04 01:01:00' },
{ unit: 10, date: '2015-12-04 01:01:00' },
{ unit: 10, date: '2015-12-04 01:31:00' },
{ unit: 5, date: '2015-12-04 01:31:00' },
{ unit: 10, date: '2015-12-04 02:01:00' },
{ unit: 5, date: '2015-12-04 02:01:00' },
{ unit: 5, date: '2015-12-04 02:31:00' },
{ unit: 9, date: '2015-12-04 02:31:00' },
{ unit: 5, date: '2015-12-04 03:01:00' },
{ unit: 9, date: '2015-12-04 03:01:00' },
{ unit: 5, date: '2015-12-04 03:31:00' },
{ unit: 10, date: '2015-12-04 03:31:00' },
{ unit: 10, date: '2015-12-04 04:01:00' },
{ unit: 5, date: '2015-12-04 04:01:00' }]
// first make an object with {date: unit} value as such
const newResults = results.reduce((acc, item) => ({
...acc,
[item.date]: (acc[item.date] || 0) + item.unit
}) , {})
console.log(newResults)
/*
{
"2015-12-04 00:01:00": 15,
"2015-12-04 00:31:00": 14,
"2015-12-04 01:01:00": 15,
"2015-12-04 01:31:00": 15,
"2015-12-04 02:01:00": 15,
"2015-12-04 02:31:00": 14,
"2015-12-04 03:01:00": 14,
"2015-12-04 03:31:00": 15,
"2015-12-04 04:01:00": 15
}
*/
// now if you want array representation for this you can do
const finalResult = Object.keys(newResults).map(key => ({date: key, unit: newResults[key]}))
console.log(finalResult)
/*
[
{
"date": "2015-12-04 00:01:00",
"unit": 15
},
{
"date": "2015-12-04 00:31:00",
"unit": 14
},
{
"date": "2015-12-04 01:01:00",
"unit": 15
},
{
"date": "2015-12-04 01:31:00",
"unit": 15
},
{
"date": "2015-12-04 02:01:00",
"unit": 15
},
{
"date": "2015-12-04 02:31:00",
"unit": 14
},
{
"date": "2015-12-04 03:01:00",
"unit": 14
},
{
"date": "2015-12-04 03:31:00",
"unit": 15
},
{
"date": "2015-12-04 04:01:00",
"unit": 15
}
]
*/
这篇关于ES6数组中按对象属性求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文