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

Tutorial on installing and using virtualenv in Deepin

virtualenv is a tool for creating isolated Python...

User experience analysis of facebook dating website design

<br />Related article: Analysis of Facebook&...

js dynamically generates tables (node ​​operations)

This article example shares the specific code of ...

Solve the problem that ElementUI custom CSS style does not take effect

For example, there is an input box <el-input r...

How to analyze SQL execution plan in MySQL through EXPLAIN

Preface In MySQL, we can use the EXPLAIN command ...

Method of implementing recursive components based on Vue technology

describe This article introduces a method to impl...

An article tells you how to write a Vue plugin

Table of contents What is a plugin Writing plugin...

vue+springboot realizes login function

This article example shares the specific code of ...

How to reset MySQL root password

Table of contents 1. Forgot the root password and...

How to implement call, apply and bind in native js

1. Implement call step: Set the function as a pro...

Mybatis mysql delete in operation can only delete the first data method

Bugs As shown in the figure, I started to copy th...

The difference between br and br/ in HTML

answer from stackflow: Simply <br> is suffic...

Advanced Usage Examples of mv Command in Linux

Preface The mv command is the abbreviation of mov...