The first type:
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. Therefore, when the array contains object members, the operation fails (the object as the key of the object will be converted into a string).
Second method:
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 the '===', but using nested loops, the performance will be worse than the first method.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn