Javascript Array count()

Description

Javascript Array count()

Array.prototype.count = function(){
    return this.length;
};

Javascript Array count()

let numbers = [1,2,3,4];
let moreNumbers = new Array(1,2,3,4);

Array.prototype.count = function () {
    return this.length;
}

console.log(numbers.count());// w w w.  j  a va  2s.  c o m
console.log(moreNumbers.count());

Javascript Array count()

Array.prototype.count = function() {
    return this.length;
}

Javascript Array count()

Array.prototype.count = function () {
    var temp = {};
    for (var i = 0; i < this.length; i++) {

        if (!(temp[this[i]])) {
            temp[this[i]] = 1;//from  w  w  w.  j  av a 2  s. c  o m
        } else {
            temp[this[i]]++;
        }
    }

    return temp;
}

Javascript Array count()

Array.prototype.count = function(){
 var j = 0;//from  ww  w.j  a v  a2  s  .c  o m
 var self = this;
 this.forEach(function(val,i){
  if(self[i]){
   j++;
  }
 })
 return j;
}



PreviousNext

Related