Skip to content

Instantly share code, notes, and snippets.

@ashishtiwari1993
Created September 25, 2020 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishtiwari1993/84119dfc87150da441e2da40e248bdbb to your computer and use it in GitHub Desktop.
Save ashishtiwari1993/84119dfc87150da441e2da40e248bdbb to your computer and use it in GitHub Desktop.
GroupBy orderBy with loadash

Use chain to apply groupBy() & orderBy() together with Loadash.

let data = [
  {"name":"foo","id":1},
  {"name":"bar","id":2},
  {"name":"foo","id":2},
  {"name":"foo","id":3},
  {"name":"bar","id":1},
  {"name":"bar","id":4},
];

_.chain(data)
  .orderBy(data => Number(data.id), ["desc"])
  .groupBy(data => data.name)
  .value();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment