[Solved] Grouping js string array with counting


This a way to do this:

var fruit = ['apple', 'apple', 'orange'];

var occurences = {};
for (var index = 0; index < fruit.length; index++) {
    var value = fruit[index];
    occurences[value] = occurences[value] ? occurences[value] + 1 : 1;
}

console.log(occurences);

The logged output is an object containing apple: 2, orange: 1} as you can see here: http://jsfiddle.net/zZ2hk/

0

solved Grouping js string array with counting