The first type:
Copy the code code as follows:
function unique (arr){
var obj = {},newArr = [];
for(var i = 0;i < arr.length;i++){
var value = arr[i];
if(!obj[value]){
obj[value] = 1;
newArr.push(value);
}
}
return newArr;
}
This method stores the value of the array into the object, so when the array exists as an object member, the operation fails (the object as the key of the object will be converted into a string).
Second method:
Copy the code code as follows:
function unique (arr){
for(var i = 0;i < arr.length;i++){
for(var j = i+1;j < arr.length;j++){
if(arr[i] === arr[j]){
arr.splice(j,1);
j--}
}
}
return arr;
}
This method is supported even if the incoming array contains objects, note '===', but using nested loops, the performance will be worse than the first method.