Javascript Array contains(obj)

Description

Javascript Array contains(obj)


Array.prototype.contains = function(obj)
{
  for(i=0;i<this.length;i+=1)
  {// w  w  w  .j a v  a 2s .co m
    if(this[i] === obj)
    {
      return true;
    }
  }
  return false;
}

Javascript Array contains(obj)

Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {//  w  ww.  j ava  2  s. c  o  m
    if (this[i] === obj) {
      return true;
    }
  }
  return false;
}

Javascript Array contains(obj)

Array.prototype.contains = function (obj) {
    return this.indexOf(obj) > -1;
};

Javascript Array contains(obj)

Array.prototype.contains = function (obj) {
    return this.indexOf(obj) >= 0;
};

Javascript Array contains(obj)

/*jshint esnext: true */

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }//from   ww w .j  a va  2 s.  co m
    }
    return false;
};

Javascript Array contains(obj)

'use strict';//  ww  w. j a v a 2  s  .co  m

Array.prototype.contains= function (obj) {
  for(let i = 0; i < this.length; i++) {
    if(this[i].key === obj) {
      return true;
    }
  }
  return false;
}
function countSameElements(collection) {
  let result = [];

  collection.filter(function (item) {
    let itemArray = collection.filter(function (value) {
      if(value === item) {
        return true;
      }
    });
    let obj = {};
    obj.key = item;
    obj.count = itemArray.length;

    if(result.contains(obj.key) === false) {
      result.push(obj);
    }
  });

  return result;
}

Javascript Array contains(obj)


//Helper function, to check if a Array contains a other object 
Array.prototype.contains = function contains(obj) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === obj) {
            return true;
        }//from  w w w. j  av a2 s. c  o  m
    }
    return false;
};



PreviousNext

Related