groupBy和sortBy本机ES6中的对象数组

时间:2018-10-12 14:50:17

标签: javascript ecmascript-6 underscore.js lodash

我们有一个对象数组,我们需要按组ID对这些对象进行分组。

然后,我们需要获取一个按时间戳对数组进行排序的数组数组。

这是初始集合:

const arr = [
  {id: 'id1', group: '123', timestamp: 222},
  {id: 'id2', group: '123', timestamp: 999 },
  {id: 'id3', group: '456', timestamp: 333 },
  {id: 'id4', group: '789', timestamp: 111 },
  {id: 'id5', group: '554', timestamp: 555 },
];

我们试图获得的结果是:

result = [
  [
    {id: 'id4', group: '789', timestamp: 111 }, 
  ],
  [
    {id: 'id1', group: '123', timestamp: 222},
    {id: 'id2', group: '123', timestamp: 999 },
  ],
  [
    {id: 'id3', group: '456', timestamp: 333 },
  ],
  [
    {id: 'id5', group: '554', timestamp: 555 },
  ]
]

我们使用Underscore.js实现了这一目标,但我们正在寻找使用本机javascript的解决方案,因此我们努力将简化的对象传递给数组数组。

var groups = _(arr).chain()
    .groupBy("group")
    .sortBy((data) => _.first(data).timestamp)
    .value();

https://jsfiddle.net/tdsow1ph/

我们尝试首先使用以下方法对项目进行分组:

const groupBy = (arr, propertyToGroupBy) => {
    return arr.reduce((groups, item) => {
      const val = item[propertyToGroupBy];

      groups[val] = groups[val] || [];
      groups[val].push(item);

      return groups;
    }, {});
  }

但是我们获得了一个不可排序的对象。因此,我们需要将其转换为array数组,但是我们很难做到这一点,然后对timestamp值应用排序。

2 个答案:

答案 0 :(得分:3)

您可以使用reduce()使用该组作为键将数组分组为一个对象。使用Object.values()将对象转换为数组。

const arr = [{"id":"id1","group":"123","timestamp":222},{"id":"id2","group":"123","timestamp":999},{"id":"id3","group":"456","timestamp":333},{"id":"id4","group":"789","timestamp":111},{"id":"id5","group":"554","timestamp":555}];

var result = Object.values(arr.reduce((c, v) => {
  c[v.group] = c[v.group] || [];
  c[v.group].push(v);
  return c;
}, {}));


console.log(result);

使用sort()

const arr = [{"id":"id4","group":"789","timestamp":111},{"id":"id1","group":"123","timestamp":222},{"id":"id3","group":"456","timestamp":333},{"id":"id5","group":"554","timestamp":555},{"id":"id2","group":"123","timestamp":999}]

var result = Object.values(
  arr
  .sort((a, b) => a.timestamp - b.timestamp)
  .reduce((c, v) => {
    c[v.group] = c[v.group] || [];
    c[v.group].push(v);
    return c;
  }, {})
);

console.log(result);

答案 1 :(得分:0)

这是ES6中使用Map objectreduce的另一行实现:

const arr = [ {id: 'id1', group: '123', timestamp: 222}, {id: 'id2', group: '123', timestamp: 999 }, {id: 'id3', group: '456', timestamp: 333 }, {id: 'id4', group: '789', timestamp: 111 }, {id: 'id5', group: '554', timestamp: 555 }, ];

const result = [...arr.reduce((r,c) => (r.set(c.group, r.has(c.group) ? 
  [...r.get(c.group), c] : [c]), r), new Map()).values()]

console.log(result)

对于以开头的sort减少的仅排序前缀:

const arr = [ {id: 'id1', group: '123', timestamp: 222}, {id: 'id2', group: '123', timestamp: 999 }, {id: 'id3', group: '456', timestamp: 333 }, {id: 'id4', group: '789', timestamp: 111 }, {id: 'id5', group: '554', timestamp: 555 }, ];

const result = [...arr.sort((a,b) => a.timestamp - b.timestamp)
   .reduce((r,c) => (r.set(c.group, r.has(c.group) ? 
   [...r.get(c.group), c] : [c]), r), new Map()).values()]

console.log(result)