Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript check how many times value in array

function f(array,value){
    var n = 0;
    for(i = 0; i < array.length; i++){
        if(array[i] == value){n++}
    }
    return n;
}
Comment

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: chartjs line color 
Javascript :: js strict mode 
Javascript :: shadow using react native 
Javascript :: flatlist onrefresh react native 
Javascript :: moment js from now 
Javascript :: how to get multiple checkbox value in jquery 
Javascript :: access css property using javascript 
Javascript :: react history.push 
Javascript :: javascript getelementbyid 
Javascript :: get element by click 
Javascript :: nodejs array buffer to buffer 
Javascript :: linked list javascript 
Javascript :: how to remove first child in javascript 
Javascript :: import all from javascript 
Javascript :: react native android padding style 
Javascript :: set header as json in laravel 
Javascript :: box shadow in react native 
Javascript :: get parameters from url 
Javascript :: node.js - How do I convert an image to a base64-encoded data URL 
Javascript :: disable key enter react-hook-form 
Javascript :: js get hostname from url 
Javascript :: js sting first letter 
Javascript :: create an attribute for html with javascript 
Javascript :: falsy value javascript 
Javascript :: react native button top right 
Javascript :: multiple click events in react 
Javascript :: chartjs lineTension 
Javascript :: mongoose increment sub document 
Javascript :: how to get console text in cypress 
Javascript :: how to fill array with consecutive numbers javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =