How to count the number of occurrences of each item in an array?

Viewed 60825

I have an array as follows,

var arr = ['ab','pq','mn','ab','mn','ab']

Expected result

arr['ab'] = 3
arr['pq'] = 1
arr['mn'] = 2

Tried as follows,

$.each(arr, function (index, value) {
    if (value) 
        arr[value] = (resultSummary[value]) ? arr[value] + 1 : 1;
});

console.log(arr.join(','));
9 Answers
Related