Skip to content

Instantly share code, notes, and snippets.

@louicoder
Created December 14, 2022 09:33
Show Gist options
  • Select an option

  • Save louicoder/513fc673ed019c1012477d1edf3cdbc2 to your computer and use it in GitHub Desktop.

Select an option

Save louicoder/513fc673ed019c1012477d1edf3cdbc2 to your computer and use it in GitHub Desktop.
Group array of objects into object
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