Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript Count the occurrences of a value in an array

const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);

// Examples
countOccurrences([2, 1, 3, 3, 2, 3], 2);                // 2
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b'], 'a');  // 3
Comment

count occurence in array js

// The initial array we want to count occurences
var initial = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];  

// The count array asked for
var count = Array.from(new Set(initial)).map(val => initial.filter(v => v === val).length);  

// Outputs [ 3, 5, 1, 1 ]
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

count occurence in array js

var set = Array.from(new Set(initial));  

//set = [5, 2, 9, 4]
Comment

PREVIOUS NEXT
Code Example
Javascript :: nuxt 18 mountend route push 
Javascript :: nombre random js 
Javascript :: conditinally add object js 
Javascript :: put two buttons in a row in react native 
Javascript :: yarn react-native-async-storage 
Javascript :: If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project. That will permanently disable this message but you might encounter other issues. 
Javascript :: Get current active sheet name google appscript 
Javascript :: jquery each data 
Javascript :: javascript json download 
Javascript :: remove disable attr jquery 
Javascript :: moment locale 
Javascript :: for key value in object javascript 
Javascript :: javascript find parent with class 
Javascript :: remove menu bar electron 
Javascript :: expressjs hello world 
Javascript :: increase-memory-limit not working node 
Javascript :: update cypress 
Javascript :: destroy chart js 
Javascript :: get select option selected text jquery 
Javascript :: ignore logs on android expo 
Javascript :: deep clone array in javascript 
Javascript :: get url query params js 
Javascript :: jquery clear click event 
Javascript :: Navigation timeout of 30000 ms exceeded 
Javascript :: fetch in js 
Javascript :: jquery datatable export button not showing 
Javascript :: how to check array is sorted or not in javascript 
Javascript :: javascript appendchild at index 
Javascript :: slider on release call api react material ui 
Javascript :: check radio button is checked jquery 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =