Created
December 14, 2022 09:33
-
-
Save louicoder/513fc673ed019c1012477d1edf3cdbc2 to your computer and use it in GitHub Desktop.
Group array of objects into object
This file contains hidden or 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 grouped = array.reduce((obj, item) => { | |
| const key = item.age; | |
| if (!obj[key]) { | |
| obj[key] = []; | |
| } | |
| obj[key].push(item); | |
| return obj; | |
| }, {}); | |
| // Log out put | |
| console.log(grouped); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment