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(','));
no need to use jQuery for this task — this example will build an object with the amount of occurencies of every different element in the array in
O(n)You could also use Array.reduce to obtain the same result and avoid a
for-loop