Javascript Array unique3()

Description

Javascript Array unique3()


Array.prototype.unique3 = function(){
 var res = [];/*from  www  . j a  v  a  2  s .c om*/
 var json = {};
 for(var i = 0; i < this.length; i++){
  if(!json[this[i]]){
   res.push(this[i]);
   json[this[i]] = 1;
  }
 }
 return res;
}
var arr = [1, 'a', 'a', 'b', 'd', 'e', 'e', 1, 0]
alert(arr.unique3());

Javascript Array unique3()

Array.prototype.unique3 = function () {
  return this.filter(function (s, i, a) {
    return i===a.lastIndexOf(s);
  })/*from  ww  w.j  ava 2 s .c  o  m*/
};



PreviousNext

Related