1function groupArrayOfObjects(list, key) {
2 return list.reduce(function(rv, x) {
3 (rv[x[key]] = rv[x[key]] || []).push(x);
4 return rv;
5 }, {});
6};
7
8var people = [
9 {sex:"Male", name:"Jeff"},
10 {sex:"Female", name:"Megan"},
11 {sex:"Male", name:"Taylor"},
12 {sex:"Female", name:"Madison"}
13];
14var groupedPeople=groupArrayOfObjects(people,"sex");
15console.log(groupedPeople.Male);//will be the Males
16console.log(groupedPeople.Female);//will be the Females
1let group = cars.reduce((r, a) => { console.log("a", a); console.log('r', r); r[a.make] = [...r[a.make] || [], a]; return r;}, {});console.log("group", group);