let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
//Set数据结构,它类似于数组,其成员的值都是唯一的
let unique = Array.from(new Set(arr));
//let unique = [...new Set(arr)]; //也可以使用展开运算符
console.log(unique); //[1, true, "true", "false", null, undefined, NaN, "NaN", {…}, {…}]
原理:Set方法会对对象的hash结果和值进行比较,如果都相等则去重,否则保留。这种方法代码量少,缺点:兼容性不是最好的,无法去除空对象。
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
let unique = [];
for (let i = 0; i < arr.length; i++) {
//indexOf等于-1说明不存在
if(unique.indexOf(arr[i]) == -1){
unique.push(arr[i]);
}
}
console.log(unique); // [1, true, "true", "false", null, undefined, NaN, NaN, "NaN", {…}, {…}]
缺点:无法去重空对象和NaN
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
let unique = [];
arr.forEach(item=>{
if(unique.indexOf(item) == -1){
unique.push(item);
}
})
console.log(unique); // [1, true, "true", "false", null, undefined, NaN, NaN, "NaN", {…}, {…}]
缺点:这种方法和for循环差不多,无法去重空对象和NaN
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
let unique = arr.filter((item,index) =>{
//当前元素在原始数组的第一个索引是否等于当前索引,等于说明该元素第一次出现则留下,不等则说明前面已经出现过则是重复的。
arr.indexOf(item) == item;
})
console.log(unique); // [1, true, "true", "false", null, undefined, "NaN", {…}, {…}]
缺点:无法去重空对象
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
arr = arr.sort();
let unique = [];
//先对数组排序,然后相邻的元素对比,不同的放到新数组里面。
for(let i = 0;i<arr.length;i++){
if(arr[i] != arr[i+1]){
unique.push(arr[i]);
}
}
console.log(unique) //[1, NaN, NaN, "NaN", {…}, {…}, "false", null, true, "true"]
缺点:无法去重空对象和NaN
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
let obj={};
let unique=[];
//将数组值作为对象的属性名,当不存在时,则放入对象中 (利用对象的属性不可重复)
for (let i = 0; i < arr.length; i++) {
if (!obj[arr[i]]) {
obj[arr[i]] = 1;
unique.push(arr[i]);
}
}
console.log(unique); // [1, true, "false", null, undefined, NaN, {…}]
缺点:可以去重NaN和空对象,但是直接把“true”、“NaN”也去掉了
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
let obj = {};
//将数组值作为对象的属性名,当不存在时,则放入对象中 (利用对象的属性不可重复)
//typeof item + item作为属性名
let unique = arr.filter((item,index)=>{
return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)
})
console.log(unique); //[1, true, "true", "false", null, undefined, NaN, "NaN", {…}]
完美去重
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
let unique = arr.reduce((prev,cur) => prev.includes(cur)?prev:[...prev,cur],[])
console.log(unique); //[1, true, "true", "false", null, undefined, NaN, "NaN", {…}, {…}]
缺点:无法去重空对象
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
let map = new Map();
let unique = [];
//因为map里面不会出现相同的key值,所以最后得到的map的key值是去重后的结果。
//判断map里面是否有某个键,没有就push到unique里面同时给map添加一个键
for(let i = 0;i<arr.length;i++){
if(!map.has(arr[i])){
map.set(arr[i],false);//false代表没有然后添加键
unique.push(arr[i]);
}
}
console.log(unique); //[1, true, "true", "false", null, undefined, NaN, "NaN", {…}, {…}]
缺点:无法去重空对象
let arr =[1,1,true,true,'true',"false","false",null,null,undefined,undefined,NaN,NaN,'NaN',{},{}];
let unique = arr
let len = unique .length;
unique.sort(function(a,b){ //排序后更加方便去重
return a - b;
})
function recursive(index){
if(index >= 1){
if(unique[index] === unique[index-1]){
unique.splice(index,1);
}
recursive(index - 1); //递归loop,然后数组去重
}
}
recursive(len-1);
console.log(unique); //[1, true, "true", "false", null, NaN, NaN, "NaN", {…}, {…}, undefined]
缺点:无法去重空对象和NaN