在Javascript中基于用户对对象进行分组

时间:2018-05-04 08:39:40

标签: javascript node.js sorting grouping

我正在尝试将从数据库收到的大量数据分组为基于用户的单独对象。

我得到的数据看起来像这样:

[ { ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:19.960Z },
  { ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:24.179Z },
  { ID: '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:18.249Z },
  { ID: '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:11.230Z },
  { ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 0,
    created: 2018-05-02T11:33:34.705Z },
  { ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:00.049Z },
  { ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:04.269Z },
  { ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:02.365Z }
]

但我希望函数的输出按照这个

的相同ID进行分组
[[{ ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:19.960Z },
{ ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:04.269Z },
  { ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:02.365Z }
],
[
  { ID: '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:18.249Z },
  { ID: '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:11.230Z }
],
[
  { ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:24.179Z },
  { ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 0,
    created: 2018-05-02T11:33:34.705Z },
  { ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 13.450980186462402,
    created: 2018-05-02T13:21:00.049Z }
]]

在javascript中执行此操作的最有效方法是什么?

我发现了一个看起来像这样的函数

function groupUserMood(xs, key) {
    return xs.reduce(function(rv, x) {
      (rv[x[key]] = rv[x[key]] || []).push(x);
      return rv;
    }, {});
  };

但这也将类似的东西加在一起,我不想要。

3 个答案:

答案 0 :(得分:3)

使用.reduce按相同的ID进行分组,然后使用Object.values来提取数组:



const input = [{
    ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
  },
  {
    ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 13.450980186462402,
  },
  {
    ID: '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b',
    value: 13.450980186462402,
  },
  {
    ID: '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b',
    value: 13.450980186462402,
  },
  {
    ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 0,
  },
  {
    ID: '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9',
    value: 13.450980186462402,
  },
  {
    ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
  },
  {
    ID: 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    value: 13.450980186462402,
  }
];
const groupedInputObj = input.reduce((accum, item) => {
  const { ID } = item;
  if (!accum[ID]) accum[ID] = [];
  accum[ID].push(item);
  return accum;
}, {});
const output = Object.values(groupedInputObj);
console.log(output);




答案 1 :(得分:2)

您可以将它们分组:

const group = arr.reduce((acc, item) => {
  if (!acc[item.ID]) {
    acc[item.ID] = [];
  }

  acc[item.ID].push(item);
  return acc;
}, {});

const result = Object.values(group);

您想要的结果将在result变量中。

答案 2 :(得分:1)

The problem of preserving the intermediate index can be solved in different ways. For example, this way:

var result = []
var keys = {}
data.forEach(function (e) {
  if (typeof keys[e.ID] === 'undefined') {
    result.push([])
    keys[e.ID] = result.length - 1
  }
  result[keys[e.ID]].push(e)
})

[ https://jsfiddle.net/d0c41Lc8/}

的data-src值