Skip to content

Instantly share code, notes, and snippets.

@nirsky
Created April 29, 2021 12:05
Show Gist options
  • Save nirsky/f6e7b12013090e8889cfa45f83601fc9 to your computer and use it in GitHub Desktop.
Save nirsky/f6e7b12013090e8889cfa45f83601fc9 to your computer and use it in GitHub Desktop.
const items = [{ key: 'a', value: 1 }, { key: 'a', value: 2 }, { key: 'b', value: 3 }, { key: 'b', value: 4 }];
const hashMap = items.reduce((acc, current) => {
if (!acc[current.key]) acc[current.key] = [];
acc[current.key].push(current.value);
return acc;
}, {});
console.log(hashMap); // { a: [ 1, 2 ], b: [ 3, 4 ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment