我有这样的数据:
const data = [
{name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12'},
{name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12'},
{name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11'},
{name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10'},
{name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01'},
{name: 'bob', colors: ['red'], count: 1, day: '2017-11-12'},
{name: 'bob', colors: [], count: 1, day: '2018-11-12'},
{name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11'},
{name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11'},
{name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12'},
];
现在,我想按天对数据进行分组并获得键计数的总和,因此获得一个像这样的对象数组:
const newData = [
{day: '2018-11-12', countSum: 5}, // sum of {name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12'}, {name: 'alice', colors: [blue'], count: 2, day: '2018-11-12'}, {name: 'bob', colors: [], count: 1, day: '2018-11-12'}, {name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12'}
{day: '2018-12-12', countSum: 1},
{day: '2018-11-11', countSum: 2}, // sum of {name: 'bob', colors: [blue'], count: 1, day: '2018-11-11'}, {name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11'}
{day: '2018-11-10', countSum: 1},
{day: '2018-11-01', countSum: 3},
{day: '2017-11-12', countSum: 1},
]
我尝试使用Lodash的groupBy
按天对数据进行分组,但是我无法计算count键的总和。
我需要帮助。
非常感谢
答案 0 :(得分:4)
您可以使用Array#reduce()
创建一个具有共同属性值(天)作为键的对象,然后使用Object.values()
返回期望的数组
const counts = data.reduce((a, {day, count}) => {
a[day] = a[day] || {day, countSum:0}
a[day].countSum += count
return a
},{})
const res = Object.values(counts)
console.log(res)
<script>
const data = [
{name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12'},
{name: 'alice', colors: ['blue'], count: 2, day: '2018-11-12'},
{name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12'},
{name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11'},
{name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10'},
{name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01'},
{name: 'bob', colors: ['red'], count: 1, day: '2017-11-12'},
{name: 'bob', colors: [], count: 1, day: '2018-11-12'},
{name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11'},
{name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11'},
{name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12'},
]
</script>
答案 1 :(得分:1)
类似于charlietfl的“减少”解决方案,您也可以将它用于..of。
const data = [
{name: 'alice', colors: ['red', 'blue'], count: 1, day: '2018-11-12'},
{name: 'duke', colors: ['red', 'blue'], count: 1, day: '2018-12-12'},
{name: 'bob', colors: ['blue'], count: 1, day: '2018-11-11'},
{name: 'alice', colors: ['blue'], count: 1, day: '2018-11-10'},
{name: 'carl', colors: ['blue'], count: 3, day: '2018-11-01'},
{name: 'bob', colors: ['red'], count: 1, day: '2017-11-12'},
{name: 'bob', colors: [], count: 1, day: '2018-11-12'},
{name: 'bob', colors: ['red', 'blue', 'yellow'], count: 1, day: '2018-11-11'},
{name: 'alice', colors: ['yellow'], count: 2, day: '2018-11-11'},
{name: 'duke', colors: ['red', 'yellow'], count: 1, day: '2018-11-12'},
];
let result = {}
for(let d of data) {
result[d.day] = result[d.day] || { day: d.day, countSum: 0}
result[d.day].countSum += d['count']
}
console.log(Object.values(result))
答案 2 :(得分:1)
这是一种lodash方法。首先 groupBy day
,然后映射该组的每个结果,并按count
在每个组上执行 sumBy ,就这样
var data = [{"name":"alice","colors":["red","blue"],"count":1,"day":"2018-11-12"},{"name":"duke","colors":["red","blue"],"count":1,"day":"2018-12-12"},{"name":"bob","colors":["blue"],"count":1,"day":"2018-11-11"},{"name":"alice","colors":["blue"],"count":1,"day":"2018-11-10"},{"name":"carl","colors":["blue"],"count":3,"day":"2018-11-01"},{"name":"bob","colors":["red"],"count":1,"day":"2017-11-12"},{"name":"bob","colors":[],"count":1,"day":"2018-11-12"},{"name":"bob","colors":["red","blue","yellow"],"count":1,"day":"2018-11-11"},{"name":"alice","colors":["yellow"],"count":2,"day":"2018-11-11"},{"name":"duke","colors":["red","yellow"],"count":1,"day":"2018-11-12"}];
var res =_(data)
.groupBy('day')
.map((grp, day) => ({day, countSum: _.sumBy(grp, 'count')}))
.value();
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>