一般的なJS配列の重量除去方法
1359 ワード
let arr = [1, 2, 5, 4, 1, 3, 8, 2, 5];
//ES6 set
//Set , Set 。 , , 。
let newArr1 = Array.from(new Set(arr)); // Array.from Set
console.log('newArr1',newArr1);
// sort() , 。
arr = arr.sort();
let newArr2 = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[i + 1]) {
newArr2.push(arr[i])
}
}
console.log('newArr2',newArr2)
//
let newArr3 = arr;
var len =newArr3.length;
newArr3.sort(function(a,b){return a-b});
function loop(index){
if(index>=1){
if(newArr3[index] === newArr3[index-1]){
newArr3.splice(index,1)
}
loop(index - 1);
}
}
loop(len-1);
console.log('newArr3',newArr3);
// for , 。 ,
let newArr4 = arr;
for( let i=0;i<newArr4,length;i++){
for(let j=i+1;j<newArr4.length;j++){
if(newArr4[i] == newArr4[j]){
newArr4.splice(j,1);
j--;
}
}
}
console.log('newArr4',newArr4)
//indexof , ,
let newArr5 = [];
for(let i=0;i<arr.length;i++){
if(newArr5.indexOf(arr[i]) ===-1){
newArr5.push(arr[i])
}
}
console.log('newArr5',newArr5)