Array.prototype.reduceintermediateconst fruits = ['apple', 'banana', 'apple', 'cherry']
fruits.reduce((acc, f) => { acc[f] = (acc[f]||0)+1; return acc }, {})
// β {apple:2, banana:1, cherry:1}Use reduce() with an object accumulator to count how many times each value appears.
reduce() can build any value β including objects. Use it to count how many times each string appears.
countOccurrences(['a', 'b', 'a']) // { a: 2, b: 1 }