JavaScript Array Deduplication Scheme

  • 2021-11-23 22:27:00
  • OfStack

Directory method 1: set: not a data type, but a data structure; Member only 1 Method 2: Object attribute name cannot be duplicated Method 3: indexOf Method 4: sort Method 5: includes: Include; If the array contains that 1 item, return true;; Does not include returning false; Method 6: hasOwnProperty: Check whether the attribute name is a private attribute of the object; Returns 1 Boolean value; Method 7: filter+indexOf Method 8: splice Method 9: Recursive Method 10: Map: Using the characteristics of Map data structure; Method 101: reduce Method 102: Similar to set of Method 1, the residual operator is used...

There are several other ways to work with arrays
- includes Method is used to determine whether an array contains a specified value, returning true if it does, or false if not.
- find Returns the 1 item found the first time
- some Returns 1 Boolean value as long as 1 is true , return true
- every Returns a Boolean value that requires true for every item to return true
- filter Returns 1 new filtered array; If true is returned, it will stay, and false will be filtered out
- reduce Convergence

Let's get down to business ~ (I hope it can help you ~ This site is a bit skinny!! Ha ha ha ha ha)

Method 1: set: It is not a data type, but a data structure; Member only 1


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

Method 2: Object property names cannot be duplicate


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 1 Items 
                i--;
                continue;
            }
            obj[cur]=cur;//  To obj Add key-value pairs; The property name and property value are 1 Like 
        }
    }
    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 a-b;
       });
       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: Include; If the array contains that 1 item, return true;; Does not include returning 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: Check whether the attribute name is a private attribute of the object; Returns 1 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 :  Array per 1 Members 
            // index:  Index corresponding to member 
            // a :  Entire array 
            // hasOwnProperty To verify whether the attribute of 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 a-b;
        });
        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: Using the characteristics of Map data structure to store values;


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 101: reduce


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 1 Items 
                i--;
                continue;
            }
            obj[cur]=cur;//  To obj Add key-value pairs; The property name and property value are 1 Like 
        }
    }
    unique(arr);

0

Method 102: Similar to set in Method 1, the residual operator is used...


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 1 Items 
                i--;
                continue;
            }
            obj[cur]=cur;//  To obj Add key-value pairs; The property name and property value are 1 Like 
        }
    }
    unique(arr);

1

Related articles: