const people = [ { name: "Kyle", age: 26 }, { name: "John", age: 31 }, { name: "Sally", age: 42 }, { name: "Jill", age: 42 }, ]; // reduce people array down to an individual result which groups by age const result = people.reduce((groupedPeople, person) => { const age = person.age; // if theres no one with this age, create array if (groupedPeople[age] == null) groupedPeople[age] = []; // add person to array by age groupedPeople[age].push(person); // return all people grouped return groupedPeople; }, {}) console.log(result)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter