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

Web project development JS function anti-shake and throttling sample code

Table of contents Stabilization Introduction Anti...

Detailed steps for manually configuring the IP address in Linux

Table of contents 1. Enter the network card confi...

Detailed explanation of mysql scheduled tasks (event events)

1. Brief introduction of the event An event is a ...

MySQL 5.7.11 zip installation and configuration method graphic tutorial

1. Download the MySQL 5.7.11 zip installation pac...

How to turn a jar package into a docker container

How to turn a jar package into a docker container...

Summary of important mysql log files

Author: Ding Yi Source: https://chengxuzhixin.com...

Problems with index and FROM_UNIXTIME in mysql

Zero, Background I received a lot of alerts this ...

Some slightly more complex usage example codes in mysql

Preface I believe that the syntax of MySQL is not...

Vue implements a simple timer component

When doing a project, it is inevitable to encount...

How to verify whether MySQL is installed successfully

After MySQL is installed, you can verify whether ...

Summary of MySQL log related knowledge

Table of contents SQL execution order bin log Wha...

Use of align-content in flex layout line break space

1. The effect diagram implemented in this article...

Understanding flex-grow, flex-shrink, flex-basis and nine-grid layout

1. flex-grow, flex-shrink, flex-basis properties ...

Installation tutorial of MySQL 5.7.17 zip package version under win10

The installation tutorial of mysql5.7.17 is share...