Let's say we have following array of objects:
const arrOfObjects = [
{
name: "A",
color: "red",
id: 1,
},
{
name: "B",
color: "blue",
id: 2,
},
{
name: "C",
color: "green",
id: 3,
},
{
name: "A",
color: "red",
id: 1,
},
{
name: "A",
color: "red",
id: 1,
},
{
name: "A",
color: "red",
id: 1,
},
{
name: "C",
color: "green",
id: 3,
},
];
I would like to reduce this array of objects by property ID and add to this object property quantity which gives information how many identical object exist in this array.
Expect result:
const reduced = [
{
name: "A",
color: "red",
id: 1,
quntitiy: 4,
},
{
name: "C",
color: "green",
id: 3,
quntitiy: 2,
},
{
name: "B",
color: "blue",
id: 2,
quntitiy: 1,
},
];
I am sure, I need to use reduce method.