Javascript Array pushUnique(k, v)

Description

Javascript Array pushUnique(k, v)

Array.prototype.pushUnique = function(k, v) {
 exist = false;/*from   w w w .  j ava2  s  .  c  o m*/
 for(var i = 0; i < this.length; i++) {
  if (this[i][k] == v) return this[i];
 }
 
 if (!exist) {
  obj = {};
  obj[k] = v;
  this.push(obj);
  return this[i++];
 }
}



PreviousNext

Related