//this my sector
sectors:[
{label: 'bb', key: 0, value: 0}
{label: 'aa', key: 33, value: 33}
{...}
]
I write code for counting duplicates in an object, but I have a problem in the last mapping section, I get this error: Cannot read properties of undefined (reading 'label') in react js
// this is my finalArray that I log
finalArray:[
{cs: '44', count: 55}
{cs: '56', count: 47}
{cs: '43', count: 41}
{cs: '27', count: 40}
{cs: '42', count: 36}
{cs: '53', count: 35}
{cs: '34', count: 32}
{cs: '70', count: 31}
{cs: '66', count: 22}
{cs: '54', count: 21}
]
// this is my code
const prepareSeries = (data, sectors) => {
let finalArray = [];
const countsByCs = {};
data.forEach(({ cs }) => {
countsByCs[cs] = (countsByCs[cs] || 0) + 1;
});
finalArray = Object.entries(countsByCs)
.map(([cs, count]) => ({ cs, count }))
.sort((a, b) => b.count - a.count)
.slice(0, 10);
console.log(finalArray);
return [
{
data: finalArray.map((item) => {
return { name: sectors.filter((sector) => sector.key == item[0])[0].label, y: item[1] };
}),
},
];
};