场景: 由于所有用户频道过多,而我的频道列表里不需要那么多,并且还有很多重复的这时候我需要用去重的方法;
注释***
userChannelList: [], //用户选择频道索引ID为0,页面刚打开是频道高亮-对应文章数据)
allChanneList: [], // 所有频道的列表 ***
在computed 计算属性 里进行去重
computed:{
//定义一个变量
unCheckChannwlList(){
//目标:把所有的频道数组挨个对象取出,去用户已经选择的频道数组里去查找,如果找不到,则让filter方法收集到一个新的数组里
const newArr= this.allChanneList.filter(bigObj=>{
//遍历每个频道对象
//寻找下标
let index=this.userChannelList.findIndex(smallObj=>{
//判断一下大数组里id 和小数组里的id 是否相等
return smallObj.id===bigObj.id
})
//如果找到了
if(index>-1){
return false
}else{
return true
}
})
return newArr
}
简洁的
computed:{
//定义一个变量
unCheckChannwlList(){
return this.allChanneList.filter(bigObj=>this.userChannelList.findIndex(smallObj=>smallObj.id===bigObj.id)==-1)
}
去重效果: