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
1var myJsonFromSQL = [{id:1, cat:'test1'},{id:2, cat:'test1'},
2 {id:3, cat:'test2'},{id:4, cat:'test2'}];
3
4// 1 LINE CODE:
5//'Set' deletes/do not consider duplicates
6const arr_cat = [...new Set(myJsonFromSQL.map(i => i.cat))];
7//RESULT:
8You will get an array with the property values grouped:
9arr_cat ['test1','test2']