标签:for val UNC mil return nbsp list ring ble
1、新建空数组,遍历旧数组,新数组中没有的就push到新数组
//1.新建空数组,遍历旧数组,新数组中没有的就push到新数组 function delRepeat(array) { var newArr = []; for(var i = 0;i < array.length; i++){ if(newArr.indexOf(array[i]) == -1){ newArr.push(array[i]); } } return newArr; } var arrlist = [1,2,52,22,1,5,2,22,5,4,6,5,8]; document.write(arrlist +"去重后:" + delRepeat(arrlist) + "<br>");
2、数组排序后剔除相同的值
//2.排序后剔除相同的值 function delRepeatBySort(array){ var sortArr = bubbleSort(array); var tempArr = [sortArr[0]]; for(var i = 1; i < sortArr.length; i++){ if(sortArr[i] !== tempArr[tempArr.length-1]){//sortArr数组的值与tempArr的最后一个值不同的就push到temp数组 tempArr.push(sortArr[i]); } } return tempArr; } var arrlist2 = [1,2,52,8,89,88,99,88,78,54,89]; document.write(arrlist2 + "去重后:" + delRepeatBySort(arrlist2) + "<br>");
3、利用对象的键值对比,遍历数组,将数组的值分别做对象的键和值,遍历数组过程中,在对象中没有的就push到新数组
//3.利用对象的键值对比 function delRepeatByObject(array){ var tempObj = {}, newArr = []; for(var i = 0; i < array.length; i++){ var val = array[i]; if(!tempObj[array[i]]){//在对象中没有 tempObj[array[i]] = array[i].toString(); newArr.push(array[i]) } } return newArr; } var arrlist3 = [1,2,88,88,2,1]; document.write(arrlist3 + "去重后:" + delRepeatByObject(arrlist3) + "<br>");
暂时只总结了这三种,以后慢慢总结补充。
-THE END-
标签:for val UNC mil return nbsp list ring ble
原文地址:https://www.cnblogs.com/menggirl23/p/10243470.html