原理:利用对象key-value进行去重
去重方法:
// 数组对象根据某一个值去重
filterList(list=[], key) {
let obj = {};
list?.forEach(item=>{
obj[item[key]]=item;
});
return Object.values(obj);
},
用法:
let list = [
{id: 1, name: '1'},
{id: 1, name: '1'},
{id: 2, name: '2'},
{id: 2, name: '2'},
{id: 3, name: '3'},
{id: 3, name: '3'},
];
console.log('去重', filterList(list, 'id'))