首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按键按数据分组,并返回包含另一个键和值的新对象。

按键按数据分组,并返回包含另一个键和值的新对象。
EN

Stack Overflow用户
提问于 2018-11-12 12:13:52
回答 3查看 73关注 0票数 0

我有这样的数据:

代码语言:javascript
复制
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'},
];

现在,我希望按数据按日分组,并获得键计数的总和,从而获得这样的对象数组:

代码语言:javascript
复制
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对数据进行分组,但无法计算计数键的总和。我需要帮助。

非常感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-11-12 12:38:14

与charlietfl的“减少”解决方案类似,您也可以使用for..of。

代码语言:javascript
复制
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))

票数 1
EN

Stack Overflow用户

发布于 2018-11-12 12:24:18

可以使用Array#reduce()创建一个以公共属性值(day)作为键的对象,然后使用Object.values()返回预期的数组。

代码语言:javascript
复制
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)
代码语言:javascript
复制
<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>

票数 4
EN

Stack Overflow用户

发布于 2018-11-12 16:32:31

下面是一种房客方法。首先groupBy day,然后映射该组的每个结果,然后由count对每个组执行sumBy,就是这样

代码语言:javascript
复制
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);
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53261979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档