Home  >  Article  >  Web Front-end  >  How to remove duplicates and reorder es6 array

How to remove duplicates and reorder es6 array

青灯夜游
青灯夜游Original
2022-05-05 19:08:193047browse

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()".

How to remove duplicates and reorder es6 array

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)));

How to remove duplicates and reorder es6 array

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);

How to remove duplicates and reorder es6 array

#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);

How to remove duplicates and reorder es6 array

[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!

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