方法一
let t=[{id:1111,name:'nnnn'},{id:1111,name:'nnnn'},{id:1111,name:'nnnn'},{id:2222,name:'mmmm'}]; let list = []; let idList = []; t.forEach((item)=>{ if(!idList.includes(item.id)){ idList.push(item.id); idList = Array.from(new Set([...idList])); list.push(item) } });
思路:把数组对象去重用数组去重作为判断
方法二
let obj = {};
t = t.reduce((item, next) =>{
if(!obj[next.id]){
obj[next.id] = item.push(next)
}
return item;
}, []);
方法二理解起来有点难度哦
方法三
let obj = {};
t.forEach((item)=>{
if(!obj[item.id]){
obj[item.id] = item.id;
list.push(item)
}
});