ymwu

导航

多种形式数组去重

let shortData=[]; //临时数组
this.dynamicTags //最后所需数组
// 数组去重展示
for (let i = 0; i < shortData.length; i++) {
//循环遍历当前数组  等于-1说明shortDatap[i]在所需数组中不存在
if (this.dynamicTags.indexOf(shortData[i]) == -1) {
this.dynamicTags.push(shortData[i]);
}
}
 
// 包含数组去重
//arr1父数组 arr2子数组
removePointById(arr1, arr2) {
for (let i = 0; i < arr2.length; i++) {
for (let j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j]) {
// console.log('输出重复的内容====》',arr1[j],'输出在父数组中的下标=====>', arr1.indexOf(arr1[j]),);
let indexs = arr1.indexOf(arr1[j]);
arr1.splice(indexs, 1);
}
}
}
// console.log('arr1======>',arr1);
return arr1;
},
 
//单个数组去重
var myArr = [1,3,4,5,6,3,7,4]; console.log(myArr.filter((value,index,arr)=>arr.indexOf(value)===index)); //[ 1, 3, 4, 5, 6, 7 ]
let array=[0,3,4,5,3,4,7,8,2,2,5,4,6,7,8,0,2,0,90];[...new Set(array)]
 
//数组中根据对象的某个树形去重
let hash = {}; let shortData=[];
shortData = shortData.reduce(function(item, next) {
hash[next.id] ? "" : (hash[next.id] = true && item.push(next));
return item;
}, []);
this.WZtableData = shortData;
 

posted on 2019-05-21 14:10  愤怒的小鸟啊啊啊  阅读(101)  评论(0编辑  收藏  举报