标签:UNC length indexof js数组 class for color 遍历 uniq
1.indexOf()
1 function unique(arr){ 2 var array = []; 3 for(var i = 0, len = arr.length; i < len; 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]
2.遍历相邻数据对比
function uniqye(arr){ var array = []; for(var i = 0,len = arr.length; i < len; 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]
标签:UNC length indexof js数组 class for color 遍历 uniq
原文地址:https://www.cnblogs.com/sener/p/13363917.html