Skip to content

Instantly share code, notes, and snippets.

@epperson
Created September 9, 2020 20:38
Show Gist options
  • Save epperson/e3e1867ae0d87c0b37a7180192bbda0c to your computer and use it in GitHub Desktop.
Save epperson/e3e1867ae0d87c0b37a7180192bbda0c to your computer and use it in GitHub Desktop.
JS Grouping
var orders = [
{ orderId: 'I8001', customerId: '5142' },
{ orderId: 'I1006', customerId: '5142' },
{ orderId: 'I1009', customerId: '5142' },
{ orderId: 'I1001', customerId: '5145' },
{ orderId: 'I9999', customerId: '5146' }
],
result = orders.reduce(function (r, a) {
r[a.customerId] = r[a.customerId] || [];
r[a.customerId].push(a);
return r;
}, Object.create(null));
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment