javascript - How to count the number of certain element in an array? -
if have array [1, 2, 3, 5, 2, 8, 9, 2]
, check how many 2
s there in array. elegant way in javascript without looping for
loop?
very simple:
var count = 0; for(var = 0; < array.length; ++i){ if(array[i] == 2) count++; }
Comments
Post a Comment