要求把几个数组合并并且去重
//合并数组,可以用contact和push,
//用push.apply会改变原来合并的数组
写题之前,先console一下contact和用push的小栗子吧!!!!
var a1 = [1,2];
var b1 = [3,4];
a1.concat(b1);
console.log(a1.concat(b1));=>[1,2,3,4]
Array.prototype.push.apply(a1,b1);
//a1.push.apply(a1,b1);
console.log(a1);=>[1,2,3,4]
console.log(b1);=>[1,2,3,4]
例题:
var arr2 = ["a","b","c"];
var arr3 = ["s","a",undefined,null];
var arr4 =["c",["d"],"s"];
var newA = arr2.concat(arr3,arr4);
var newA1 = newA.reduce((prev,cur)=>{
if(!prev.includes(cur)){
return prev.concat(cur);
}else{
return prev;
}
},[])
console.log(newA1);