js 数组去重 + 数组内元素为对象去重
1、普通数组去重
利用Set
[...new Set([1,2,2,3,3,4,4,5,5,4,3,2,1])]
输出:[1,2,3,4,5]
2、数组内元素为对象去重
function Es6duplicate(arr,type){
if(arr.length == 0){
return arr;
}else{
if(type){
var obj = {}
var newArr = arr.reduce((cur,next) => {
obj[next.name] ? "" : obj[next.name] = true && cur.push(next);
return cur;
},[])
return newArr;
}else{
return Array.from(new Set(arr));
}
}
}