I have an array of strings that have duplicate values:
const = ['apple', 'orange', 'apple', 'apple', 'pear', 'pear']
What I would like to return is the following:
let occurrences = [{fruit: 'apple', numberOfOccurences: 3},{fruit: 'orange', numberOfOccurences: 1}, {fruit: pear, numberOfOccurences: 2}]
I have tried reduce
and nested for
loops but it is not returning the results I am expecting.