1 根据某些属性去重,用到数组的find方法
arr=[
{
maxDeptCode: "md3"
maxDeptName: "泡泡"
minDeptCode: "md301"
minDeptName: "泡泡少儿"
schoolId: 1
schoolName: "北京"
},
{
maxDeptCode: "md2"
maxDeptName: "中学"
minDeptCode: "md201"
minDeptName: "中学一对一"
schoolId: 1
schoolName: "北京"
},{
maxDeptCode: "md3"
maxDeptName: "泡泡"
minDeptCode: "md301"
minDeptName: "泡泡少儿"
schoolId: 1
schoolName: "北京"
},
]
function process(arr) {
// 缓存用于记录
const cache = [];
for (const t of arr) {
// 检查缓存中是否已经存在
if (cache.find(c => c.maxDeptCode === t.maxDeptCode && c.minDeptCode === t.minDeptCode)) {
// 已经存在说明以前记录过,现在这个就是多余的,直接忽略
continue;
}
// 不存在就说明以前没遇到过,把它记录下来
cache.push(t);
}
// 记录结果就是过滤后的结果
return cache;
}
转载自 https://www.cnblogs.com/lzy190917/p/11540707.html