Methods to remove duplicates and sort: 1. Use the "Array.from(new Set(arr))" or "[...new Set(arr)]" statement to remove duplicate elements in the array and return The new array after deduplication; 2. Use sort() to sort the deduplication array, the syntax is "deduplication array.sort()".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
es6 method to remove duplicates and reorder the array:
1. Remove duplicate elements from the array
1) You can use Array.from(new Set(arr))
statement
var arr = [1, 2, 3, 2, 3,4,5,4]; console.log(arr); console.log(Array.from(new Set(arr)));
2) You can use […new Set (arr)]
Statement
var arr = [5,2,1,34,1,3,2,5,2]; var newArr=[...new Set(arr)]; console.log(newArr);
#2. Reorder the deduplicated array
You can use sort( ) method to sort.
The sort() method is used to sort the elements of an array.
The sort order can be alphabetical or numerical, and in ascending or descending order. The default sort order is ascending alphabetically.
newArr.sort(); console.log(newArr);
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of How to remove duplicates and reorder es6 array. For more information, please follow other related articles on the PHP Chinese website!