• Welcome to the eight year blog, I'm glad to meet you at the right age!
  • Due to the theme, QQ login partners will display the default avatar in the comments. Please go to the personal center and upload the avatar again.

ES6 uses set to implement array de duplication

Code Notes barben 3 months ago (04-03) 162 views 0 comments

After, in《 Understanding the set structure in ES6 》In this article, we said that the new set structure in ES6 is similar to array, but there are no duplicate values. So we can use this feature to implement the operation of array de duplication.

Use the array. From method

First, convert the array to set type, remove the duplicate values, and then use array. From to convert it back into an array.

 var arr = [1, 4, 3, 3, 2, 2, 3];

var newArr = Array.from(new Set(arr));
console.log(newArr); // [1, 4, 3, 2]

Use extension operator method

Similarly, the array is converted to set type first, removing duplicate values, and then using the extension operator "..." Turn it into a comma separated parameter sequence and put it into an empty array at last!

 var arr = [1, 4, 3, 3, 2, 2, 3];

var newArr = [...new Set(arr)];
console.log(newArr); // [1, 4, 3, 2]


Eight blogs that year, we've been there all the time
If the author does not indicate the original article, please indicate the link and source of this article
ES6 using set to achieve array de duplication - https://www.barben.cn/code/759.html
Like it( two )
Post my comments
Cancel comment
expression Mapping Bold Strikethrough Center Italics

You need to bring your nickname and email with you in the year of eight!

  • Nickname (required)
  • Email address (required)
  • website