我有这样的数据:
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对数据进行分组,但无法计算计数键的总和。我需要帮助。
非常感谢
发布于 2018-11-12 12:38:14
与charlietfl的“减少”解决方案类似,您也可以使用for..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))
发布于 2018-11-12 12:24:18
可以使用Array#reduce()创建一个以公共属性值(day)作为键的对象,然后使用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>
发布于 2018-11-12 16:32:31
下面是一种房客方法。首先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>
https://stackoverflow.com/questions/53261979
复制相似问题