Count occurrences of array elements with JavaScript -
this question has answer here:
i have javascript array length 129.
var fullnames = [karri, ismo, grigori, ahmed, roope, arto .....]
i find how many times names appeared in array , store information in array this:
var counter = [2, 5, 7, ..]
where karri occured in fullnames
array 2 times, ismo occured 5 times etc. ideas how it?
i assuming fullnames
array of strings. if so, can so:
var occurences = { }; (var = 0; < fullnames.length; i++) { if (typeof occurences[fullnames[i]] == "undefined") { occurences[fullnames[i]] = 1; } else { occurences[fullnames[i]]++; } } console.log(occurences); // prints out like: {"karri": 2, "ismo": 5, ...}
Comments
Post a Comment