I want to counting duplicate item in my array of object,I usse this code below but I have this error : //TypeError: Cannot read properties of undefined (reading 'type')
//this is my array
data[
1:{cs:'10', name:'a' , age},
2:{cs:'20', name :'b', age:'25'},
3:{cs:'10', name :'h', age:'51'},
4:{cs:'10', name :'g', age:'30'},
...]
//this is my result that i want
finalArray[
{cs:'10', count :3},
{cs:'20', count :1 },
...]
TypeError: Cannot read properties of undefined (reading 'type')
const prepareSeries = (data, sectors) => {
let finalArray = [{}];
const map = new Map();
finalArray.forEach(function (stockItem) {
if (map.has(stockItem.cs)) {
map.get(stockItem.cs).count++;
} else {
map.set(stockItem.cs, Object.assign(stockItem, { count: 1 }));
}
});
finalArray = [...map.values()];
const result = Object.entries(finalArray)
.sort((a, b) => b[1] - a[1])
.slice(0, 10);
return [
console.log(result),
];
};