var arr = [2,1,4,3,2,4,2,3,4,2,6,5,5]
var obj = {};
var arrNew = [];
for(var i=arr.length-1;i>=0;i--){
if(!obj[arr[i]]){
obj[arr[i]] = true;
arrNew.push(arr[i]);
}
}
console.log(arrNew,obj)
// [5, 6, 2, 4, 3, 1]
Array.prototype.unique2 = function(){ this.sort(); //先排序 var res = [this[0]]; for(var i = 1; i < this.length; i++){ if(this[i] !== res[res.length - 1]){ res.push(this[i]); } } return res; } var arr = [1, 'a', 'a', 'b', 'd', 'e', 'e', 1, 0] console.log(arr.unique2());