Javascript Array count(val)

Description

Javascript Array count(val)


// Counting number of occurences of something in array,
// not the length of it.
Array.prototype.count = function(val){
  var occurrences = 0;
  for (var i = 0; i < this.length; i++) {
    if(this[i] === val)
    occurrences++;/* w  ww  . j a  v  a  2  s . co  m*/
  }
  return occurrences;
};



PreviousNext

Related