Home > Article > Web Front-end > How to determine whether an array is repeated in es6
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.
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 length
attribute 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{ }
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{ }
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!