JS array deduplication method: 1. "new Set()" method provided by ES6; 2. "filter()" method; 3. Use for loop with "indexOf()" method; 4. , compare each element of the array with other elements in turn, and delete duplicate elements; 5. Create a new empty array, use the "indexOf()" method to determine the index of the current element in the array, if it is the same as the subscript of the loop is added to the new array; 6. Double for loop; 7. "includes()" method.
The operating system of this tutorial: windows10 system, JavaScript ECMAScript 2023 version, DELL G3 computer.
1. Use the Set structure new Set() provided by ES6. It is easy to use and highly recommended.
Directly add it to a new array and use the extension operator of es6.
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; console.log(arr); function noRepeat(arr){ var newArr = [...new Set(arr)]; //利用了Set结构不能接收重复数据的特点 return newArr } var arr2 = noRepeat(arr) console.log(arr2);
2. Use filter() to remove duplicates
The filter() method creates a new array. The elements in the new array are checked for all elements in the specified array that meet the conditions. item is the value of the current element, and index is the index value of the current element. The indexOf() method returns the first occurrence of a specified string value in a string. Use indexOf() to query the subscript of the array to see if it is equal to the current subscript. If equal, return it, otherwise no value will be returned.
var arr = ['apple','apps','pear','apple','orange','apps']; console.log(arr) var newArr = arr.filter(function(item,index){ return arr.indexOf(item) === index; // 因为indexOf 只能查找到第一个 }); console.log(newArr);
3. Use for loop with indexOf to remove duplicates
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; function noRepeat(arr) { //定义一个新的临时数组 var newArr=[]; //遍历当前数组 for(var i=0;i<arr.length;i++) { //如果当前数组的第i已经保存进了临时数组,那么跳过, //否则把当前项push到临时数组里面 if(newArr.indexOf(arr[i]) === -1) { //indexOf() 判断数组中有没有字符串值,如果没有则返回 -1 newArr.push(arr[i]); } } return newArr } var arr2 = noRepeat(arr); console.log(arr2);
4. Compare each element of the array with other elements in turn and find duplicate elements. Deletion is cumbersome and not recommended
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; console.log(arr); function noRepeat(arr) { for(var i = 0; i < arr.length-1; i++){ for(var j = i+1; j < arr.length; j++){ if(arr[i]===arr[j]){ arr.splice(j,1); j--; } } } return arr; } var arr2 = noRepeat(arr); console.log(arr2);
5. Use the new array to determine the index of the current element in the array through the indexOf method. If it is equal to the subscript of the loop, add it to the new array
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; console.log(arr) function noRepeat(arr) { var newArr = []; for (var i = 0; i < arr.length; i++) { if (arr.indexOf(arr[i]) == i) { newArr.push(arr[i]); } } return newArr; } var arr2 = noRepeat(arr); console.log(arr2);
6. Use a double for loop
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; console.log(arr); function noRepeat(arr){ for (var i = 0; i < arr.length; i++) { for (var j = 0; j < arr.length; j++) { if (arr[i] == arr[j] && i != j) { //将后面重复的数删掉 arr.splice(j, 1); } } } return arr; } var arr2 = noRepeat(arr); console.log(arr2);
7. Use includes to implement array deduplication
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; function noRepeat(arr) { let newArr = []; for(i=0; i<arr.length; i++){ if(!newArr.includes(arr[i])){ newArr.push(arr[i]) } } return newArr } console.log(noRepeat(arr));
The above is the detailed content of How to remove duplicates from js array. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment