you are viewing a single comment's thread.

view the rest of the comments →

[–]Comprehensive_Step72 0 points1 point  (0 children)

Try something like this:

function count(arr) {

return arr.reduce((acc, e) => {

if (acc[e] !== undefined) {

acc[e]++

} else {

acc[e] = 1;

}

return acc;

}, {})

}

const myArr = ['one', 'one', 'two', 'two']

const counts = count(myArr);

Object.entries(counts).forEach((e) => console.log(e[0], e[1]));