I try many ways to solve this. I can remove duplicate values. but the problem is updating total_product value. How can I do this? Give me some hints.
This is my Main Array :
const colors = [
{ "name": "Black", "total_product": 1 },
{ "name": "Black", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 },
]
I Want Like This :
const colors = [
{ "name": "Black", "total_product": 2 },
{ "name": "White", "total_product": 4 }
]
CodePudding user response:
It's a good case for reduce
which "boils down" an array to an accumulator value. Here, reduce (doc) can be used to create objects that count occurrences of each name...
const colors = [
{ "name": "Black", "total_product": 1 },
{ "name": "Black", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 }
];
const counts = colors.reduce((acc, obj) => {
if (!acc[obj.name]) acc[obj.name] = { name: obj.name, total_product: 0 };
acc[obj.name].total_product ;
return acc;
}, {});
const result = Object.values(counts);
console.log(result);
The intermediate value counts
produced by reduce looks like this...
{
"Black": {
"name": "Black",
"total_product": 2 // <- incremented each time we found a "Black"
},
"White": {
"name": "White",
"total_product": 4 // <- incremented each time we found a "White"
}
}
Object.values()
, like the name implies, makes an array of the values.
CodePudding user response:
You can use new Map()
or simple object to store the count of same objects:
const colors = [
{ "name": "Black", "total_product": 1 },
{ "name": "Black", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 },
{ "name": "White", "total_product": 1 },
];
const colorsMap = new Map();
colors.forEach(color => {
if (colorsMap.has(color.name)) {
colorsMap.set(color.name, colorsMap.get(color.name) 1);
return;
}
colorsMap.set(color.name, 1);
});
const groupedColors = [];
colorsMap.forEach((value, key) => {
groupedColors.push({
name: key,
total_product: value
});
})
console.log(groupedColors);
CodePudding user response:
It makes no sense to return an Array since you already squashed the array to unique names and their respective values, simply reduce to Object:
const colors = [
{name:"Black", total_product:1},
{name:"Black", total_product:1},
{name:"White", total_product:1},
{name:"White", total_product:1},
{name:"White", total_product:1},
{name:"White", total_product:1},
];
const sumColors = colors.reduce((ob, item) => {
ob[item.name] ??= 0
ob[item.name] = item.total_product;
return ob;
}, {});
console.log(sumColors); // {"Black": 2, "White": 4}
CodePudding user response:
const colors = [
{ name: "Black", total_product: 1 },
{ name: "Black", total_product: 1 },
{ name: "White", total_product: 1 },
{ name: "White", total_product: 1 },
{ name: "White", total_product: 1 },
{ name: "White", total_product: 1 }
];
const uniqueColors = (colors) => {
const unique = [];
colors.map((color) => {
const index = unique.findIndex((c) => c.name === color.name);
if (index === -1) {
unique.push(color);
} else {
unique[index].total_product ;
}
});
return unique;
};
console.log(uniqueColors(colors));