Created
April 29, 2021 12:05
-
-
Save nirsky/f6e7b12013090e8889cfa45f83601fc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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