Home > Article > Web Front-end > How to remove duplicates from front-end html array
This article mainly introduces the method of deduplicating front-end html arrays. It has certain reference value. Now I share it with you. Friends in need can refer to it
Array deduplication
Knowledge points used:
1:indexOf()
This method returns the index value of the first occurrence of element in the array;
If there is, the index value will be returned normally;
If the retrieved content does not exist in the array , then return -1
2: for loop
Exercise: Array deduplication
//The first method
##
var aList = [1,2,3,4,4,3,2,1,2,3,4,5,6,5,5,3,3,4,2,1]; var aList2 = []; for (var i = 0 ; i < aList.length ; i ++) { var value = aList[i] if (aList.indexOf(value) + 1) { console.log('重复了',value) } else { aList2.push(value) } } console.log(aList2)
// 第二种方法 var aList = [1,2,3,4,4,3,2,1,2,3,4,5,6,5,5,3,3,4,2,1]; for(var i=0;i<aList.length;i++) { if(aList.indexOf(aList[i])==i) { aList2.push(aList[i]); } } alert(aList2);
// 第三种方法 var aList = [1,2,3,4,4,3,2,1,2,3,4,5,6,5,5,3,3,4,2,1]; for (var i = 0; i < aList.length; i++) { var item = aList[i] if (newArray.indexOf(item) == -1) { newArray.push(item) } else { console.log('重复了',item) } }Above That’s the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website! Related recommendations:
The above is the detailed content of How to remove duplicates from front-end html array. For more information, please follow other related articles on the PHP Chinese website!