count number of times an element is occuring in an array in javascript
Array.prototype.frequencies = function() {
var l = this.length, result = {all:[]};
while (l--){
result[this[l]] = result[this[l]] ? ++result[this[l]] : 1;
}
// all pairs (label, frequencies) to an array of arrays(2)
for (var l in result){
if (result.hasOwnProperty(l) && l !== 'all'){
result.all.push([ l,result[l] ]);
}
}
return result;
};
var freqs = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4].frequencies();
alert(freqs[2]); //=> 5
// or
var freqs = '1,1,2,one,one,2,2,22,three,four,five,three,three,five'
.split(',')
.frequencies();
alert(freqs.three); //=> 3
Also in JavaScript:
- moment.set
- js in_array
- javascript includes
- Uncaught TypeError: jQuery(...).jqGrid is not a function
- discord js check if person banned
- vscode entenstion build
- jquery add element to array
- how to disable menu bar in browser using javascript
- angular serve
- node js read file from s3 bucket
- constructor react
- what is local storage and session storage in javascript
- react router dom npm
- pug to html
- ajax django send array
- js catch all images errors
- generate module with routing in angular
- javascript pass parameter to event listener
- js get date in ms
- get syntethicbaseevent and parameter in react
- array index javascript show only first 2 elements
- react native dynamic view size
- faker javascript name escape apostrophe
- javascript append to paragraph