• Welcome to Baben's blog that year. I'm glad to meet you at the right age!
  • Due to the theme, the QQ login partner displays the default avatar in the comments. Please go to the personal center to upload the avatar again.

ES6 Use Set to Realize Array De duplication

Code Notes barben 4 years ago (2020-04-03) 2980 views 0 comments

Following, on《 Understand the Set structure in ES6 》In the article, we said that the new Set structure in ES6 is similar to an array, but there is no duplicate value. So we can use this feature to implement the array de duplication operation.

Use the Array.from method

First convert the array to Set type to remove duplicate values, and then use Array. from to convert it back to 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 the extension operator... method

Similarly, first convert the array to Set type to remove duplicate values, then use the extension operator "..." to convert it to a comma separated parameter sequence, and finally put it into an empty array!

 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 have been
If the author does not indicate that it is an original article, please indicate the link and source of this article for reprinting
ES6 Use Set to Realize Array De duplication- https://www.barben.cn/code/759.html
Like( four )
Post my comments
Cancel comment
expression Mapping Bold Strike through Center Italic

You need to bring your nickname and email with you in the review of Eight Books that year!

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