Home  >  Article  >  Web Front-end  >  How to determine whether an array is repeated in es6

How to determine whether an array is repeated in es6

青灯夜游
青灯夜游Original
2022-04-19 17:51:224027browse

Judgment method: 1. Use the length attribute to obtain the length of the original array; 2. Use "[...new Set(arr)]" to remove duplicate elements in the array and return a new array; 3. Use length The attribute obtains the length of the array after deduplication; 4. Use "==" to compare whether the lengths of the arrays obtained twice are equal. If they are equal, the arrays are not repeated.

How to determine whether an array is repeated in es6

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

es6 Method to determine whether an array is repeated

Implementation idea:

  • Use The lengthattribute gets the length of the original array

  • Use[...new Set(arr)]to remove duplicate elements from the array

  • Use the length attribute to obtain the length of the array after deduplication

  • Use the == operator to compare whether the lengths of the arrays obtained twice are equal. If they are equal, the arrays are not repeated.

Implementation code:

var arr = [1,2,3,3,4,5];
len1=arr.length;
newArr=[...new Set(arr)];
len2=newArr.length;
if(len1==len2){
	console.log("数组没有重复");
}else{
}

How to determine whether an array is repeated in es6

It can be seen that the arrays in the above example are repeated.

var arr = [1,2,3,4,5];
len1=arr.length;
newArr=[...new Set(arr)];
len2=newArr.length;
if(len1==len2){
	console.log("数组没有重复");
}else{
}

How to determine whether an array is repeated in es6

It can be seen that the array in the above example is not repeated.

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

The above is the detailed content of How to determine whether an array is repeated in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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