Home > Web Front-end > JS Tutorial > How to remove duplicates from a javascript array

How to remove duplicates from a javascript array

青灯夜游
Release: 2023-01-07 11:47:32
Original
2296 people have browsed it

Method: 1. Use the "[...new Set(arr)]" statement; 2. Use the "Array.from(new Set(arr))" statement; 3. Use the filter and indexOf functions; 4. Use double for loops to check whether the values ​​are duplicated. If there are duplicates, use push() to delete them.

How to remove duplicates from a javascript array

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method 1: […new Set(arr)]

const arr = [1, 2, 3, 2, 3];
[...new Set(arr)]; // [1, 2, 3]
Copy after login

Method 2: Array.from(new Set(arr))

const arr = [1, 2, 3, 2, 3];
Array.from(new Set(arr)); // [1, 2, 3]
Copy after login

Since the elements in the Set are unique, whether they are original values ​​or object references, deduplication can be achieved by converting the array into a Set object.

The Array.from method can convert the Set Object is converted into an array.

Method 3: Using filter indexOf

function unique(arr) {
  return arr.filter(function(item, index, arr) {
    //当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素
    return arr.indexOf(item, 0) === index;
  });
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
//[1, "true", true, 15, false, undefined, null, "NaN", 0, "a", {…}, {…}]
Copy after login

Method 4: Double for loop

The easiest method to understand, outside The layer loop traverses the elements, and the inner loop checks whether there are duplicates

When there are duplicate values, you can use push() or splice()

function distinct(a, b) {
    let arr = a.concat(b);
    for (let i=0, len=arr.length; i<len; i++) {
        for (let j=i+1; j<len; j++) {
            if (arr[i] == arr[j]) {
                arr.splice(j, 1);
                // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一
                len--;
                j--;
            }
        }
    }
    return arr
}
Copy after login

But this method takes up more memory Higher, but also the lowest efficiency

How to remove duplicates from a javascript array

Method 5: for...of includes()

Upgrade of double for loop version, replace the for loop with the for...of statement in the outer layer, and change the inner loop to includes()

First create an empty array, and when includes() returns false, push the element into an empty array

Similarly, you can also use indexOf() instead of includes()

function distinct(a, b) {
    let arr = a.concat(b)
    let result = []
    for (let i of arr) {
        !result.includes(i) && result.push(i)
    }
    return result
}
Copy after login

How to remove duplicates from a javascript array

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to remove duplicates from a javascript array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template