Detailed discussion of several methods for deduplicating JavaScript arrays

Detailed discussion of several methods for deduplicating JavaScript arrays

1. Set Deduplication

 function funSet(arr){
   return Array.from(new Set(arr));
 }

2. Double for loop to remove duplicates

function funFor(arr){
  for(let i=0,len=arr.length;i<len;i++){
    for(let j=i+1,len=arr.length;j<len;j++){
      if (arr[i]===arr[j]){
        arr.splice(j,1);
        len--;
        j--;
      }
    }
  }
  return arr;
}

3. Use indexOf to remove duplicates

function funIndex(arr){
  let newArr = [];
  for(let i=0;i<arr.length;i++){
    if (newArr.indexOf(arr[i])===-1){
      newArr.push(arr[i])
    }
  }
  return newArr;
}

4. Use icludes to remove duplicates

function funInclude(arr){
  let newArr = [];
  for(let i=0;i<arr.length;i++){
    if (!newArr.includes(arr[i])){
      newArr.push(arr[i])
    }
  }
  return newArr;
}

5. Filter

function funFilter(arr){
  return arr.filter(function(item,index){
    return arr.indexOf(item,0)===index;
  })
}

6. Map

function funMap(arr){
  let map = new Map();
  let newArr = [];
  for(let i=0,len=arr.length;i<len;i++){
    if (map.has(arr[i])){
      map.set(arr[i],true);
    }else{
      map.set(arr[i],false);
      newArr.push(arr[i]);
    }
  }
  return newArr;
}

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

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
  • JavaScript array deduplication solution
  • Detailed explanation of JavaScript array deduplication
  • Seven ways to implement array deduplication in JS

<<:  How to realize vertical arrangement of text using CSS3

>>:  Several practical scenarios for implementing the replace function in MySQL

Recommend

Introduction to HTML_PowerNode Java Academy

What is HTML? HTML is a language used to describe...

Recommend several MySQL related tools

Preface: With the continuous development of Inter...

The difference between MySQL database stored procedures and transactions

Transactions ensure the atomicity of multiple SQL...

mysql5.7 create user authorization delete user revoke authorization

1. Create a user: Order: CREATE USER 'usernam...

CentOS 7 method to modify the gateway and configure the IP example

When installing the centos7 version, choose to co...

Docker container connection implementation steps analysis

Generally speaking, after the container is starte...

Building command line applications with JavaScript

Table of contents 1. Install node 2. Install Comm...

An article to show you how to create and use Vue components

Table of contents 1. What is a component? 2. Crea...

Several practical scenarios for implementing the replace function in MySQL

REPLACE Syntax REPLACE(String,from_str,to_str) Th...

v-for directive in vue completes list rendering

Table of contents 1. List traversal 2. The role o...