js 去数组重复值
作者:互联网
var arr = [1, 3, 2, 5, 2, 1, 4, 2, 1];
arr = arr.filter(function(item,index,array){
return array.indexOf(item) === index;
});
console.log(arr);
Array.prototype.unique3 = function(){
var res = [];
var json = {};
for(var i = 0; i < this.length; i++){
if(!json[this[i]]){
res.push(this[i]);
json[this[i]] = 1;
}
}
return res;
}
var arr = [112,112,34,'你好',112,112,34,'你好','str','str1'];
alert(arr.unique3());
function unite(arr1, arr2, arr3) { //数组拆嵌套再去重
for(var i=1;i<arguments.length;i++)
{
arr1.push(arguments[i]);
}
const flatten = (arr) => {
return arr.reduce(
function(acc,val) {
return acc.concat(Array.isArray(val) ? flatten(val) : val);
},[]);
};
arr1 = flatten(arr1);
// var obj = {};
// var array = [];
function unique(array1){
return array1 = array1.filter(function(item,index,array){
return array1.indexOf(item) === index;
});
}
arr1 = unique(arr1);
return arr1;
}
console.log(unite([1, 3, 2], [5, 2, 1, 4], [2, 1]));
标签:function,arr,return,arr1,重复,js,数组,112,var 来源: https://blog.csdn.net/YUlangML/article/details/121093185