标签:== size filter ble asc 方法 opera span elf
var arr = [0,2,3,4,4,0,2];
var obj = {};
var tmp = [];
for(var i = 0 ;i< arr.length;i++){
if( !obj[arr[i]] ){
obj[arr[i]] = 1;
tmp.push(arr[i]);
}
}
console.log(tmp);
结果如下: [0, 2, 3, 4]
方法二:
var arr = [2,3,4,4,5,2,3,6],
arr2 = [];
for(var i = 0;i< arr.length;i++){
if(arr2.indexOf(arr[i]) < 0){
arr2.push(arr[i]);
}
}
console.log(arr2);
结果为:[2, 3, 4, 5, 6]
方法三:
var arr = [2,3,4,4,5,2,3,6];
?
var arr2 = arr.filter(function(element,index,self){
?
return self.indexOf(element) === index;
?
});
?
console.log(arr2);
结果为:[2, 3, 4, 5, 6]
方法四:
var arr = [1,2,3,4,2,3];
var arr2 = [];
for(var i = 0;i< arr.length;i++){
if(!arr2.includes(arr[i])){
arr2.push(arr[i]);
}
}
console.log(arr2);
结果为:[1, 2, 3, 4]
方法五:ES6
提供了新的数据结构 Set
。它类似于数组,但是成员的值都是唯一的,没有重复的值。
function unique(arr) {
return [
}
var arr = [1,2,3,3,3,2];
unique(arr);
结果为:[1, 2, 3]
标签:== size filter ble asc 方法 opera span elf
原文地址:https://www.cnblogs.com/guojiayu/p/11192984.html