Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

count number of each element in array javascript

var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
var counts = {};

for (var i = 0; i < arr.length; i++) {
  var num = arr[i];
  counts[num] ? counts[num] + 1 : 1;
}

console.log(counts[5], counts[2], counts[9], counts[4]);
Comment

PREVIOUS NEXT
Code Example
Javascript :: chrome storage onchanged 
Javascript :: nodejs csv to json from link 
Javascript :: return empty new promise 
Javascript :: js escape characters 
Javascript :: firestore batch add array 
Javascript :: React CKEditor Custom build 
Javascript :: js concat variable and string 
Javascript :: vue 3 computed 
Javascript :: emotion react 
Javascript :: angular refresh token 
Javascript :: set image size phaser 
Javascript :: flutter regular expression for arabic and english characters 
Javascript :: isInt js 
Javascript :: convert date format from yyyy-mm-dd to dd-mm-yyyy using value javascript 
Javascript :: typescript read json file 
Javascript :: jquery fadein display new page 
Javascript :: how to delete element in list javascript 
Javascript :: convert days into year, Month, days 
Javascript :: react add inline styles in react 
Javascript :: set package json to install latest version 
Javascript :: sort javascript number array with duplicates 
Javascript :: javascript prime number 
Javascript :: How to lock Orientation for a particular screen in ios in react native 
Javascript :: react axios get cookie from response 
Javascript :: jquery onload function for div 
Javascript :: number object js 
Javascript :: how to find largest number in array in javascript 
Javascript :: how to get attr in vuejs 
Javascript :: find all subsets of an array javascript 
Javascript :: history push search params 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =