const groupBy = (arr, key) =>
arr.reduce((acc, i) => {
(acc[i[key]] = acc[i[key]] || [] ).push(i);
return acc;
}, {});
const data = [
{
nom: "coka",
cat: "Boisson",
prix: 0.4,
},
{
nom: "carotte",
cat: "Legume",
prix: 9,
},
{
nom: "farine",
cat: "Epicerie",
prix: 4,
},
{
nom: "cafe",
cat: "Petit-dej",
prix: 4,
},
{
nom: "jus de fruit",
cat: "Boisson",
prix: 2,
},
{
nom: "choux",
cat: "Legume",
prix: 4.3,
},
{
nom: "olives",
cat: "Epicerie",
prix: 60,
},
{
nom: "veau",
cat: "Boucherie",
prix: 40,
},
{
nom: "poireau",
cat: "Legume",
prix: 2,
}
];
let z = groupBy(data,"cat");