I have this array with duplicate values, i get these values from an API, the ccode below gets all the notes of maths without duplicate notes using the …new Set() method :
let notes = [];
if (props.getAllNotes()) {
const maths = props.getAllNotes().map(item => {
return item.subjects[0].math_note
});
notes = [...new Set(maths)];
/*const counts = stars.reduce((acc, value) => ({
...acc,
[value]: (acc[value] || 0) + 1
}), {});*/
}
This is what i have in the props.getAllNotes() :
notes = [15,16,10,13,15,16,10,18,11,13,15,16,10,18,11];
And this is what i get :
notes = [15,16,10,13,18,11];
I want to add the count of each note in the final array notes like this for ex :
notes = [{10: 3}, {15, 5}...]
The commented method does it in object, i need to do it to the final array notes in which i using the …new Set() method because i am mapping through it to render some data
const counts = stars.reduce((acc, value) => ({
...acc,
[value]: (acc[value] || 0) + 1
}), {});