stefan22
12/5/2018 - 12:26 AM

Sorts Object Array by Gender √ with reduce

Sorts an object array by gender using reduce - pretty nice √

let data = [
  {name: "Raphel", gender: "male"},
  {name: "Tom", gender: "male"},
  {name: "Jerry", gender: "male"},
  {name: "Dorry", gender: "female"},
  {name: "Suzie", gender: "female"},
  {name: "Dianna", gender: "female"},
  {name: "Prem", gender: "male"},
];

let breakByGender = data.reduce((acc, item) => {
  acc[item.gender].push(item);
  return acc;

}, {male: [], female: []});






console.log(breakByGender);