Home Web Front-end Front-end Q&A How to implement arr (array) deduplication in es6

How to implement arr (array) deduplication in es6

Aug 30, 2022 pm 05:48 PM
javascript es6 es6 array

3 implementation methods: 1. Set data structure and "Array.from()" deduplication, syntax "Array.from(new Set(arr))"; 2. Set data structure and extension operator "..." to remove duplicates, the syntax is "[...new Set(arr)]"; 3. filter() and indexOf() filtering, the syntax is "arr.filter((it,in)=>{return arr .indexOf(it,0)===in;});".

How to implement arr (array) deduplication in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

5 ways to remove duplicates from ES6 arrays

1. Set data structure and Array.from() Repeat

  • #Set is a new data structure provided by ES6, which is similar to an array, but has no duplicate values. Using this feature, we can convert the array to a Set type for deduplication, and then use the Array.from method to convert it to an array again.

  • The Array.from method is used to convert two types of objects into real arrays: array-like objects and iterable objects (including ES6 new Additional data structures Set and Map).

Implementation idea:

  • After converting the array into a set collection to remove duplication, use the Array.from method to convert the set into an array

Syntax:

Array.from(new Set(arr))
Copy after login

Example:

let arr=[1,2,3,3,2,"1",0,undefined,undefined];
let newArr=Array.from(new Set(arr));
console.log(newArr);
Copy after login

How to implement arr (array) deduplication in es6

2. Set data structure and extended operations The operator "..." is used to remove duplicates

  • The expansion operator is introduced in ES6 and expands the iterable object into its separate elements. , the so-called iterable object is any object that can be traversed using a for of loop, such as: array, string, Map, Set, DOM node, etc.

Implementation idea:

  • After converting the array into a set collection to remove duplication, use the spread operator... to expand the set into an array, and Convert a collection to an array

Syntax:

[...new Set(arr)]
Copy after login

Example:

let arr=[1,2,3,3,2,"1",0,1,2];
let newArr=[...new Set(arr)];
console.log(newArr);
Copy after login

How to implement arr (array) deduplication in es6

3. Use arrays The filter indexOf method removes duplicates

The filter() method creates a new array, and the elements in the new array are checked by checking all elements in the specified array that meet the conditions.

The indexOf method returns the first index (index) of the specified element in the array, if not, returns -1

Example:

var arr=[1, 2, 3, 2, 3];
var newArr = arr.filter((item,index)=> {
  return arr.indexOf(item,0) === index;
});
console.log(newArr);
Copy after login

How to implement arr (array) deduplication in es6

So the index values ​​returned by each element in the arr array here through the indexOf() method are 0 1 2 1 2

arr.forEach(item => console.log(arr.indexOf(item))); // 0 1 2 1 2
Copy after login

You can use indexOf to achieve deduplication, such as in arr The fourth element 2 returns an index of 1 through indexOf, but its current index subscript is 3, which is not equal, indicating that the current 2 element has appeared before, so it should be filtered out.

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of How to implement arr (array) deduplication in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

See all articles