I am grappling with the challenge of grouping an array of JSON objects by the id_commande
attribute and eliminating any duplicates. I am finding it quite perplexing to figure out a solution to this issue without relying on a library like lodash. Ideally, I would like to tackle this problem using plain JavaScript.
let commandList = [
{
id_commande: 18,
date: "2020-12-07",
etat: "en traitement",
nom: "Tom Kha Gai",
prix_total: 16.68,
quantite: 1,
},
{
id_commande: 18,
date: "2020-12-07",
etat: "en traitement",
nom: "Tom Yum Talai",
prix_total: 16.68,
quantite: 1,
},
{
id_commande: 19,
date: "2020-12-07",
etat: "en traitement",
nom: "Tom Yum Gai",
prix_total: 16.1,
quantite: 1,
},
{
id_commande: 19,
date: "2020-12-07",
etat: "en traitement",
nom: "Tom Yum Tai",
prix_total: 16.1,
quantite: 1,
}
];
The desired output is :
let combined = [{
id_commande: 18,
date: "2020-12-07",
etat: "en traitement",
plats: [{
nom: "Tom Kha Gai",
quantite: 1
},
{
nom: "Tom Yum Talai",
quantite: 1
}
],
prix_total: 16.68
},
{
id_commande: 19,
date: "2020-12-07",
etat: "en traitement",
plats: [{
nom: "Tom Yum Gai",
quantite: 1
},
{
nom: "Tom Yum Tai",
quantite: 1
}
],
prix_total: 16.1
},
]