• 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.

Understand the Set structure in ES6

Code Notes barben 4 years ago (2020-04-02) 2762 views 0 comments

What is a Set?

ES6 adds a new data structure called Set. It is similar to an array, but it does not duplicate values.

What can be the parameters of new Set()?

1. Array

2. Objects similar to arrays
For example, the NodeList collection returned by the DOM operation and the arguments object inside the function.

 //NodeList object document.querySelectorAll('p'); //Arguments object function args() { return arguments; }


3. It can even be a string

Attention

1. In the Set data structure, there is a size attribute, which can be regarded as the length of the array.

2. The Set data structure has no key name, that is, you cannot use the index to obtain a value like an array.

Convert Set structure to array

Although Set is similar to array, many methods in array cannot be used on Set. So sometimes you need to convert it into an array.

use Array.from method
This method is used to convert two types of objects into real arrays:
·Array like object
·Traversible objects (including the new data structures Set and Map in ES6)

For more information about Array.from, click here

Set Instance Method


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
Understand the Set structure in ES6- https://www.barben.cn/code/707.html
Like( three )
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