js版本
let pesonList = [{id:0,name:'111'},{id:0,name:'111'},{id:1,name:'111'},{id:0,name:'111'},]]
let obj = {} cur = []
let pes = pesonList.reduce((cur,next) => {
obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
return cur;
},[])
ts封装版本
/**
* ArrorSet
* 数组对象去重
* arr数组数据,setType需要筛选的字段
*/
const ArrorSet = (arr: any, setType: any) => {
console.log('arr', arr, setType);
let obj: any = {};
let ArrorSetList = arr.reduce((cur: any, next: any) => {
obj[next[setType]] ? '' : (obj[next[setType]] = true && cur.push(next));
return cur;
}, []);
return ArrorSetList;
};
网友评论