数组去重排序
created(){
this.arrayDeReordering();
this.arrayDeReorderingTwo();
this.arrayDeReorderingThree();
this.arrayDeReorderingFour();
},
methods:{
// 数组去重排序
// 比大小
sortMethod(arrayData){
return (arrayData.sort(function (x,y) {
return x-y;
}))
},
// 双层循环,外层循环元素,内层循环时比较值
// 如果有相同的值则跳过,不相同则push进数组
arrayDeReordering(){
let arrayData = [14,3,6,8,28,6,5,13,14,1];
let result = [];
let len = arrayData.length;
for(let i=0;i<len;i++){
for(let j=i+1;j<len;j++){
if(arrayData[i] === arrayData[j]){
j = ++i;
}
}
result.push(arrayData[i]);
}
console.log(this.sortMethod(result))
return this.sortMethod(result);
},
// 利用新数组indexOf查找
arrayDeReorderingTwo(){
let arrayData = [14,3,6,8,28,6,5,13,14,1];
let result = [];
let len = arrayData.length;
for(let i=0;i<len;i++){
if(result.indexOf(arrayData[i])==-1){
result.push(arrayData[i]);
}
}
console.log(this.sortMethod(result))
return this.sortMethod(result);
},
// 利用对象的属性不能相同的特点进行去重
arrayDeReorderingThree(){
let arrayData = [14,3,6,8,28,6,5,13,14,1];
let result = [];
let len = arrayData.length;
let obj = {};
for(let i=0;i<len;i++){
if(!obj[arrayData[i]]){
obj[arrayData[i]] = 1;
result.push(arrayData[i]);
}
}
console.log(this.sortMethod(result))
return this.sortMethod(result);
},
//es6 set
arrayDeReorderingFour(){
let arrayData = [14,3,6,8,28,6,5,13,14,1];
console.log(this.sortMethod([...new Set(arrayData)]))
return this.sortMethod([...new Set(arrayData)]);
}
}
原文:https://blog.csdn.net/qq_36711388/article/details/79488291