The modified answer address two issues with IE.
- IE does not support arrow functions
- IE does not support the spread operator
So the logic is pretty simple. If the make key is not in the result, put a new empty array in the result for the key. And then just push the value to it.
var cars = [{
make: "audi",
model: "r8",
year: "2012"
},
{
make: "audi",
model: "rs5",
year: "2013"
},
{
make: "ford",
model: "mustang",
year: "2012"
},
{
make: "ford",
model: "fusion",
year: "2015"
},
{
make: "kia",
model: "optima",
year: "2012"
}
];
var group = cars.reduce(function(r, a) {
r[a.make] = (r[a.make] || []);
r[a.make].push(a);
return r;
}, {});
console.log(group);
1
solved IE does not support the spread operator (…) how to fix this issue