JS数组去重
2021-04-10 23:27
标签:UNC length indexof js数组 class for color 遍历 uniq 1.indexOf() 2.遍历相邻数据对比 JS数组去重 标签:UNC length indexof js数组 class for color 遍历 uniq 原文地址:https://www.cnblogs.com/sener/p/13363917.html 1 function unique(arr){
2 var array = [];
3 for(var i = 0, len = arr.length; i ){
4 if(array.indexOf(arr[i]) === -1){
5 array.push(arr[i]);
6 }
7 }
8 return array;
9 }
10 var arr = [0,0,0,1,3,2,3,5,3,2];
11 console.log(unique(arr));
12 //[0,1,3,2,5]
function uniqye(arr){
var array = [];
for(var i = 0,len = arr.length; i ){
if(arr[i] !== arr[i-1]){
array.push(arr[i]);
}
}
return array;
}
var arr = [1,1,1,2,4,5,2,3,3,2,5];
console.log(unique(arr));
//[1,2,4,5,3]