Javascript Array pushUnique(obj, equalityFunction)

Description

Javascript Array pushUnique(obj, equalityFunction)


Array.prototype.pushUnique = function (obj, equalityFunction) {
  equalityFunction  = (typeof equalityFunction === "undefined") ? function(a, b) { return a === b } : equalityFunction;
  for(var n = 0; n < this.length; n++) {
    if(equalityFunction(this[n], obj)) { // contains
      return false;
    }//from www  .  j a  v  a2s .co  m
  }
  this.push(obj);
  return true;
}



PreviousNext

Related