Home > Web Front-end > Front-end Q&A > How to implement array deduplication in es5 and es6

How to implement array deduplication in es5 and es6

青灯夜游
Release: 2023-01-16 17:09:44
Original
1690 people have browsed it

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax is "for(i=0;i

How to implement array deduplication in es5 and es6

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

es5 array deduplication

##indexOf method

Use indexOf (can return the position where a specified string value first appears in the string, if it appears for the first time, return -1)

	var arr = [1,2,3,1,3,4,5];
	Array.prototype.myInfo = function(){
		var newArr = [];
		for(var i=0;i<arr.length var if newarr return console.log><p><img src="https://img.php.cn/upload/article/000/000/024/356e180a6b80e6d664a8d03b344e4adc-0.png" alt="How to implement array deduplication in es5 and es6"></p>
<h2>
<a id="es6__21"></a>es6 Array deduplication<strong></strong>
</h2>
<p><a id="1_Arrayfrom__Set_22"></a><strong>1. Array.from and Set<span style="font-size: 16px;"></span></strong></p>Set does not allow duplicate elements<p></p>
<pre class="brush:php;toolbar:false">	let arr = [1,2,1,2,3];
	let result = new Set(arr);

	console.log(result); 			
	console.log(Array.from(result));
Copy after login

How to implement array deduplication in es5 and es6

## 2. rest and Setrest method operator is "

"

Strips characters from the array

	let arr = [1,2,1,2,3];
	let result = new Set(arr);

	console.log(result); 	
	console.log(...result); 		
	console.log([...result]); 			

	console.log([...new Set(arr)]);
Copy after login

How to implement array deduplication in es5 and es6【Related recommendations:

javascript video tutorial

, web front-end

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template