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 componentd component internal event callback and pain point analysis

Table of contents Written in front What exactly i...

Pure HTML+CSS to achieve typing effect

This article mainly introduces the typing effect ...

How to choose the right MySQL datetime type to store your time

When building a database and writing a program, i...

How InnoDB cleverly implements transaction isolation levels

Preface In the previous article Detailed Explanat...

The difference between html, xhtml and xml

Development Trends: html (Hypertext Markup Languag...

Detailed explanation of Mysql function call optimization

Table of contents Function call optimization Func...

CentOS7 configuration Alibaba Cloud yum source method code

Open the centos yum folder Enter the command cd /...

Detailed explanation of the method of comparing dates in MySQL

If there is a table product with a field add_time...

Detailed explanation of fetch network request encapsulation example

export default ({ url, method = 'GET', da...

Example of implementing QR code scanning effects with CSS3

Online Preview https://jsrun.pro/AafKp/ First loo...

In-depth explanation of environment variables and configuration files in CentOS

Preface The CentOS environment variable configura...

How to make a div height adaptive to the browser height

This old question has troubled countless front-end...