[{ name: "cat" }, { name: "cat" }]
, стало [{ name: "cat", count: 2 }]
. const result = {};
const arr = [{ name: "cat" }, { name: "cat" }];
arr.forEach(function(item){
if(result[item.name]) {
result[item.name].count += 1;
} else {
result[item.name] = {...item, count: 1};
}
})
console.log(result) // { cat: { name: 'cat', count: 2 } }
console.log(Object.values(result)) // [{ name: "cat", count: 2 }]
console.log(arr) // [{ name: "cat" }, { name: "cat" }];