I am trying to merge objects of an array into one if they have the same key.
Having this array:
let data = [
{ id: "1", cars: 5 },
{ id: "1", pasta: 2 },
{ id: "2", cars: 0 },
{ id: "2", pasta: 0 },
];
I would like to achieve this:
let data = [
{ id: "1", cars: 5, pasta: 2 },
{ id: "2", cars: 0, pasta: 0 },
];
I am looking for any solutions, but preferable ES6.
question from:
https://stackoverflow.com/questions/65875293/merge-objects-of-an-array-into-one-object-by-key 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…