JavaScript array deduplication solution

JavaScript array deduplication solution

There are several other ways to process arrays~
- includes : The method is used to determine whether an array contains a specified value. Depending on the situation, it returns true if it contains it, otherwise it returns false.
- find : Returns the first item found
- some : Returns a Boolean value, if one of them is true , it returns true
- every : Returns a Boolean value. It requires every item to be true to return true.
- filter : Returns a new filtered array; if true is returned, it is kept, and false is filtered out
- reduce :convergence

Now let’s get to the point~ (I hope this helps you~ I’m a bit naughty!! Hahahahahaha)

Method 1: set: It is not a data type, but a data structure; members are unique

let arr = [12,1,12,3,1,88,66,9,66];
function unique(ary) {
        let s = new Set(ary);
        // Array.from: Convert the set data structure into a real array;
        return Array.from(s)
    }
    unique(arr);

Method 2: Object attribute names cannot be repeated

let arr = [12,1,12,3,1,88,66,9,66];
function unique(ary) {
        let obj = {};
        for(let i=0;i<ary.length;i++){
            let cur = ary[i];
            if (obj[cur]) {
                //ary.splice(i,1);// causes the array to collapse ary[i]=ary[ary.length-1];
                ary.length--; // delete the last item i--;
                continue;
            }
            obj[cur]=cur; // Add a key-value pair to obj; the attribute name and attribute value are the same}
    }
    unique(arr);

Method 3: indexOf

let arr = [12,1,12,3,1,88,66,9,66];
 function unique(ary) {
        let newAry = [];
        for(let i=0;i<ary.length;i++){
            let cur = ary[i];
            if (newAry.indexOf(cur)===-1){
                newAry.push(cur);
            }
        }
        return newAry;
    }
    unique(arr)

Method 4: sort

let arr = [12,1,12,3,1,88,66,9,66];
function unique(ary) {
       let a = ary.sort(function (a,b) {
           return ab;
       });
       for(let i=0;i<a.length;i++){
           if(a[i]===a[i+1]){
               a.splice(i+1,1);
               i--;
           }
       }
       return a;
   }
   unique(arr)

Method 5: includes: includes; if the array contains that item, returns true; if it does not contain it, returns false;

let arr = [12,1,12,3,1,88,66,9,66];
function unique(ary) {
        let newAry = [];
        let len ​​= ary.length;
        for(let i=0;i<len;i++){
            let cur = ary[i];
            if (!newAry.includes(cur)){
                newAry.push(cur);
            }
        }
        return newAry;
    }
    console.log(unique(arr));

Method 6: hasOwnProperty: Checks whether the property name is a private property of the object; returns a Boolean value;

let arr = [12,1,12,3,1,88,66,9,66];
function unique(ary) {
        let obj = {};
        return ary.filter(function (item,index,a) {
            // item: each member of the array // index: the index corresponding to the member // a: the entire array // hasOwnProperty is used to check whether the property has appeared;
           return obj.hasOwnProperty(typeof item+item)?false:obj[typeof item+item]=true;
           if (obj.hasOwnProperty(typeof item+item)){
               return false
           }else{
               obj[typeof item+item]=true;
               return true;
           }
        })
    }
    console.log(unique(arr))

Method 7: filter+indexOf

let arr = [12,1,12,3,1,88,66,9,66];
    function unique(ary) {
        return ary.filter(function (item,index,a) {
            return ary.indexOf(item)===index;
        })
    }
    console.log(unique(arr));

Method 8: splice

let arr = [12,1,12,3,1,88,66,9,66];
 function unique(ary) {
        for(let i=0;i<ary.length;i++){
            for(j=i+1;j<ary.length;j++){
                if(ary[i]===ary[j]){
                    ary.splice(j,1);
                    j--;
                }
            }
        }
        return ary;
    }
    unique(arr);

Method 9: Recursion

let arr = [12,1,12,3,1,88,66,9,66];
function unique(ary) {
        let len ​​= ary.length;
        ary = ary.sort(function (a,b) {
            return ab;
        });
        function loop(index) {
            if(index>=1){
                if(ary[index]===ary[index-1]){
                    ary.splice(index,1);
                }
                loop(index-1)
            }
        }
        loop(len-1);
        return ary;
    }
    console.log(unique(arr));

Method 10: Map: Utilizes the value storage feature of Map data structure;

let arr = [12,1,12,3,1,88,66,9,66];
function unique(ary) {
        let newAry = [];
        let map = new Map();
        for(let i=0;i<ary.length;i++){
            if (!map.has(ary[i])){
                map.set(ary[i],true);
                newAry.push(ary[i]);
            }
        }
    }
    unique(arr);

Method 11: reduce

let arr = [12,1,12,3,1,88,66,9,66];
function unique(ary) {
        // reduce: The first one is a function, and the second parameter will be passed to prev of the first callback;
        return ary.reduce((prev,next)=>{
            //The return value of this function is prev for the next execution;
            return prev.includes(next)?prev:[...prev,next];
        },[])
    }
    console.log(unique(arr));

Method 12: Similar to method 1, using the rest operator...

let arr = [12,1,12,3,1,88,66,9,66];
    let a = [...new Set(arr)];
    console.log(a);

This is the end of this article about JS array solutions. For more relevant JS array deduplication content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Five common ways to remove duplicate arrays in JavaScript
  • Introduction to JavaScript array deduplication and flattening functions
  • JS array deduplication details
  • Detailed discussion of several methods for deduplicating JavaScript arrays
  • Detailed explanation of JavaScript array deduplication
  • Seven ways to implement array deduplication in JS

<<:  A brief analysis of MySQL parallel replication

>>:  UDP connection object principle analysis and usage examples

Recommend

Web designers should optimize web pages from three aspects

<br />With the increase of bandwidth, there ...

Detailed analysis and usage of tcpdump command under Linux

Introduction To put it simply, tcpdump is a packe...

How to view the running time of MySQL statements through Query Profiler

The previous article introduced two methods to ch...

Detailed explanation of Tomcat configuration and optimization solutions

Service.xml The Server.xml configuration file is ...

22 Vue optimization tips (project practical)

Table of contents Code Optimization Using key in ...

VMware Workstation Pro installs Win10 pure version operating system

This article describes the steps to install the p...

Five things a good user experience designer should do well (picture and text)

This article is translated from the blog Usability...

Install Centos7 using Hyper-v virtual machine

Table of contents introduce Prepare Download syst...

A brief analysis of MySQL parallel replication

01 The concept of parallel replication In the mas...

Zen coding resource update function enhancement

Official website: http://code.google.com/p/zen-cod...

canvas.toDataURL image/png error handling method recommendation

Problem background: There is a requirement to tak...

A brief discussion on the principle of shallow entry and deep exit of MySQL

Table of contents 1. Overview of the page 2. Infi...

Node.js makes a simple crawler case tutorial

Preparation First, you need to download nodejs, w...