There are many ways to deduplicate arrays in js, but today we will use the latest function of es6 to achieve a more concise solution. The two methods to be introduced now are related to es6’s new data structure Set. Let’s briefly introduce Set first. ES6 provides a new data structure Set. It is similar to an array, but the values of the members are unique and there are no duplicate values.
Set itself is a constructor used to generate Set data structure. Can accept an array as parameter for initialization.
Use Set combined with expander
const set = new Set([1, 2, 3, 4, 4]);[...set]// ==> [1, 2, 3, 4]
Use Set combined with Array.form, Because the data type returned by new Set() is not a data type, Array.form() is used to format and convert it into an ordinary array.
const set = Array.form(new Set([1, 2, 3, 4, 4]));// ==> [1, 2, 3, 4]
Related recommendations:
Six ways to share JS array deduplication
detailed explanation of js array deduplication and deflattening
Detailed examples of several ideas for deduplicating javascript arrays
The above is the detailed content of Detailed explanation of js array deduplication example. For more information, please follow other related articles on the PHP Chinese website!