Judgment method: 1. Convert the array to a Set collection, and use the size attribute to obtain the total number of Set elements, with the syntax "new Set(arr).size"; 2. Use the length attribute to obtain the total number of array elements; 3. Compare the total number of Set elements and the array Whether the total number of elements is equal, if they are not equal, they contain the same value, otherwise they do not.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
In es6, you can use Set to determine whether there are the same elements in the array.
ES6 provides a new data structure Set. The values of the members in the Set are unique and there are no duplicate elements.
Method to determine whether there are the same elements in the array:
Convert the array to a Set, and use the size attribute to return the total number of current Set elements
to determine whether the total number of Set elements is equal to the total number of array elements
let arr = [1,2,3,4,5]; if(new Set(arr).size !== arr.length){ console.log("Have the same elements--------Yes"); } else { console.log("No identical elements------No"); }