I have an array of javascript objects that are products. These products are displayed in a list as a cart.
I want to count the number of duplicate products in the array based in the _.id value and remove these objects from the array and relace them with an updated version and a new key called count with a value of the total number of times this object comes up.
So far, I have tried numerous methods and I've searched all over google but there's nothing that I've found that can do the job correctly.
An example of the type of array that I will be using would be this:
[
{ _id: "5971df93bfef201237985c4d",
slug: "5971df93bfef201237985c4d",
taxPercentage: 23,
totalCost: 9.99,
currency: "EUR",
},
]
so what I would want my end result to be would be something like this - it removes the duplicate value and replaces it with the same object but adds a new key called count with a value of the number of times the object initially was in the array:
[
{ _id: "5971df93bfef201237985c4d",
slug: "5971df93bfef201237985c4d",
taxPercentage: 23,
totalCost: 9.99,
currency: "EUR",
count: 2, // whatever the count is
},
]
So far I'm using this method:
var count = [];
if (cart.cart.products != undefined) {
let namestUi = {
renderNames(names){
return Array.from(
names.reduce( (counters, object) =>
counters.set(object._id, (counters.get(object._id) || 0) + 1),
new Map() ),
([object, count]) => {
var filterObj = names.filter(function(e) {
return e._id == object;
});
return ({filterObj, count})
}
);
}
};
count = namestUi.renderNames(cart.cart.products);
console.log(count)
}
but it returns the values like this:
{filterObj: Array // the array of the duplicates, count: 2}
{filterObj: Array, count: 1}
and since I am using React-Native with a list view something like this won't work.
It just needs to store the items the way it was before (an array) but with a new child called count.
Any help is welcomed!