简体   繁体   中英

How to group and sort object array?

I've got an object array which I have to group and sort:

[
  {
    id: 123,
    group: 'abc',
    metadata: {
      name: 'tom'
    },
    date: ISODate("2019-07-08T20:33:40.475Z")
  },
  {
    id: 456,
    group: 'def',
    metadata: {
      name: 'bob'
    },
    date: ISODate("2019-07-08T20:33:40.475Z")
  },
  {
    id: 789,
    group: 'def',
    metadata: {
      name: 'bob'
    },
    date: ISODate("2019-07-10T20:33:40.475Z")
  },
  {
    id: 234,
    group: 'ghi',
    metadata: {
      name: 'frank'
    },
    date: ISODate("2019-07-10T20:33:40.475Z")
  },
  {
    id: 567,
    group: 'abc',
    metadata: {
      name: 'tom'
    },
    date: ISODate("2019-07-10T20:33:40.475Z")
  }
]

Firstly I need to group the elements by the group value, then I need to sort the elements of this grouped array by date.

For grouping I've tried this:

const result = array.reduce(function (r, a) {
    r[a.group] = r[a.group] || [];
    r[a.group].push(a);
    return r;
}, Object.create(null));

But the result is not as expected and the elements are not sorted by date.

The result could/should look something like this:

[
  [
    {
      id: 123,
      group: 'abc',
      metadata: {
        name: 'tom'
      },
      date: ISODate("2019-07-08T20:33:40.475Z")
    },
    {
      id: 567,
      group: 'abc',
      metadata: {
        name: 'tom'
      },
      date: ISODate("2019-07-10T20:33:40.475Z")
    }
  ],
  [
    {
      id: 456,
      group: 'def',
      metadata: {
        name: 'bob'
      },
      date: ISODate("2019-07-08T20:33:40.475Z")
    },
    {
      id: 789,
      group: 'def',
      metadata: {
        name: 'bob'
      },
      date: ISODate("2019-07-10T20:33:40.475Z")
    }
  ],
  [
    {
      id: 234,
      group: 'ghi',
      metadata: {
        name: 'frank'
      },
      date: ISODate("2019-07-10T20:33:40.475Z")
    }
  ]
]

Use reduce to group the values, and sort to sort them, using map as well due to the nested array structure.

 const arr = [{id:123,group:'abc',metadata:{name:'tom'},date:"2019-07-08T20:33:40.475Z"},{id:456,group:'def',metadata:{name:'bob'},date:"2019-07-08T20:33:40.475Z"},{id:789,group:'def',metadata:{name:'bob'},date:"2019-07-10T20:33:40.475Z"},{id:234,group:'ghi',metadata:{name:'frank'},date:"2019-07-10T20:33:40.475Z"},{id:567,group:'abc',metadata:{name:'tom'},date:"2019-07-10T20:33:40.475Z"}]; const res = Object.values(arr.reduce((a, { group, ...r }) => { (a[group] = a[group] || []).push({ group, ...r }); return a; }, {})).map(e => e.sort(({ date: a }, { date: b }) => new Date(a) - new Date(b))); console.log(res); 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

(I've taken out the ISODate function and just kept the string date to make the snippet executable.)

I would suggest to use lodash .

var ISODate = function(str) { return str };

var data = [
  {
    id: 123,
    group: 'abc',
    metadata: {
      name: 'tom'
    },
    date: ISODate("2019-07-08T20:33:40.475Z")
  },
  {
    id: 456,
    group: 'def',
    metadata: {
      name: 'bob'
    },
    date: ISODate("2019-07-08T20:33:40.475Z")
  },
  {
    id: 789,
    group: 'def',
    metadata: {
      name: 'bob'
    },
    date: ISODate("2019-07-10T20:33:40.475Z")
  },
  {
    id: 234,
    group: 'ghi',
    metadata: {
      name: 'frank'
    },
    date: ISODate("2019-07-10T20:33:40.475Z")
  },
  {
    id: 567,
    group: 'abc',
    metadata: {
      name: 'tom'
    },
    date: ISODate("2019-07-10T20:33:40.475Z")
  }
]

console.log(
  _.mapValues(
    _.groupBy(data, 'group'),
    function(item) { return _.orderBy(item, ['date'], ['asc']) }
  )
)

https://codepen.io/anon/pen/bPzmOr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM