1.es3函数
//判断一个元素是否存在于某个数组内
function exist(element,array) {
/*array.forEach(function (curr) {
if (curr === element){
return true;
}
})
return false;*/
for (var i = 0;i < array.length;i++) {
if (array[i] === element) {
return true;
}
}
return false;
}
var aki = [];
var ramai = [1,"momo","nagi",1,"ohana","moegumi","suki","omae","momo"];
//aki空数组中不存在的就往里面添加,返回的新数组就是需要的去重的数组
function del(arr) {
arr.forEach((curr,index) => {
if (index == 0) {
aki.push(curr);
} else if (!exist(curr,aki)){
aki.push(curr);
}
})
return aki;
}
del(ramai);
console.log(aki)
2.filter瞎几把用
let haha = [1,2,32,14,15,"ww",435,32,2,7,"ww",8];
let strArr = [];
let ret = haha.filter((item) => {
let str = (typeof item) + item;
//console.log(str)
if(!strArr.includes(str)) {
strArr.push(str);
return true;
}
})
console.log(ret)
console.log(strArr) //(9) ["number1", "number2", "number32", "number14", "number15", "stringww", "number435", "number7", "number8"]
filter只关注你给我返回true还是false,true就往ret里加
3.ES5
var oo = [1,2,3,1,'a',1,'a'].filter(function(ele,index,array){
return index===array.indexOf(ele)
})
console.log(oo)
4.ES6
var haha = [...new Set([1,2,3,1,'a',1,'a'])]
console.log(haha)