I need to group by my elements in array so I did in that way:
const cars = [{
make: "vw",
model: "passat",
year: "2012"
},
{
make: "vw",
model: "golf",
year: "2013"
},
{
make: "ford",
model: "mustang",
year: "2012"
},
{
make: "ford",
model: "fusion",
year: "2015"
},
{
make: "kia",
model: "optima",
year: "2012"
}
];
let group = cars.reduceRight((r, a) => {
r[a.make] = [...r[a.make] || [], a];
return r;
}, []);
console.log(cars)
console.log(group)
But unfortunatelly after that grouping with reduce
the order of elements is wrong.
VW Group is at the end, how can I fource reduce to not sorting elements, or maybe there is better way to group elements?
DEMO: https://www.w3schools.com/code/tryit.asp?filename=GG4TVX5J3X3A