//数组去重
// 方法1
var add = [1,2,3,2,1,4,5,8,5,6,2,3,5];
console.log(add.sort());
// console.log(add);
for(var i = 0;i<add.length;i++){
if(add[i]==add[i+1]){
add.splice(i--,1)
}
}
console.log(add);
//方法2
var add1 = [1,2,3,2,1,4,5,8,5,6,2,3,5];
var num = [];
for(var i=0 ;i<add1.length;i++){
if(num.indexOf(add1[i])===-1){
num.push(add1[i])
}
}
console.log(num);
//方法3
var add2 = [1,2,3,2,1,4,5,8,5,6,2,3,5];
console.log(Array.from(new Set (add2)));
JS数组去重
最新推荐文章于 2024-04-02 18:05:32 发布