简体   繁体   中英

Lodash: How to group, then sort collection by more than one property

How can I group, then sort my collection by more than one property?

My collection looks like

const list = [
  {
    transactionId: "Id1",
    amount: 100,
    timestamp: 1619075704230,
    user: "username1",
  },
  {
    transactionId: "Id2",
    amount: 5,
    timestamp: 1619075704230,
    user: "username2",
  },
  {
    transactionId: "Id3",
    amount: 8,
    timestamp: 1619075704230,
    user: "username3",
  },
  {
    transactionId: "Id4",
    amount: 12,
    timestamp: 1619075704230,
    user: "username1",
  },
  {
    transactionId: "Id5",
    amount: 200,
    timestamp: 1619075704230,
    user: "username7",
  },
  {
    transactionId: "Id6",
    amount: 99,
    timestamp: 1619075704230,
    user: "username1",
  },
  {
    transactionId: "Id7",
    amount: 78,
    timestamp: 1619075704230,
    user: "username1",
  },
  {
    transactionId: "Id8",
    amount: 12,
    timestamp: 1619075704230,
    user: "username9",
  },
];

I want to group the list by user attribute, and then sort sort the received arrays by timestamp and amount fields.

Exactly response should be like

{
....
  "username1": [
    {
      transactionId: "Id1",
      amount: 100,
      timestamp: 1619075704230,
      user: "username1",
    },
    ,
    {
      transactionId: "Id6",
      amount: 99,
      timestamp: 1619075704230,
      user: "username1",
    },
    {
      transactionId: "Id7",
      amount: 78,
      timestamp: 1619075704230,
      user: "username1",
    },
    {
      transactionId: "Id4",
      amount: 12,
      timestamp: 1619075704230,
      user: "username1",
    }
  ]
  ...
}

You can do it by using groupBy and orderBy functions.

const list = [
  {
    transactionId: "Id1",
    amount: 100,
    timestamp: 1619075704230,
    user: "username1",
  },
  {
    transactionId: "Id2",
    amount: 5,
    timestamp: 1619075704230,
    user: "username2",
  },
  {
    transactionId: "Id3",
    amount: 8,
    timestamp: 1619075704230,
    user: "username3",
  },
  {
    transactionId: "Id4",
    amount: 12,
    timestamp: 1619075704230,
    user: "username1",
  },
  {
    transactionId: "Id5",
    amount: 200,
    timestamp: 1619075704230,
    user: "username7",
  },
  {
    transactionId: "Id6",
    amount: 99,
    timestamp: 1619075704230,
    user: "username1",
  },
  {
    transactionId: "Id7",
    amount: 78,
    timestamp: 1619075704230,
    user: "username1",
  },
  {
    transactionId: "Id8",
    amount: 12,
    timestamp: 1619075704230,
    user: "username9",
  },
];

const sorted = _.orderBy(list, ['timestamp', 'amount'], ['desc', 'desc']);

const result = _.groupBy(sort, 'user');

console.log('Result: ', result);

If you use lodash

You can do something like

let grouped = _.groupBy(list,'user');

let ordered = {};

for (const [key, value] of Object.entries(grouped)) {
  ordered[key] = _.orderBy(value,['timeStamp','amount']);
}

console.log(orderedArray);

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