This is the 15th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

The problem

Interviewer: Can you tell me something about ES6 set?

Interviewer: Barra, it’s mostly used to recycle arrays and strings and things like that.

Interviewer: Can you talk about weakSet? What’s the difference?

Interviewer:…

The above questions are purely fictitious and may be similar.

Today, we will talk about the usage of Set and weakSet.

Set

Its internal values are unique and not duplicated. If duplicate data is passed in, the result is deduplicated data.

So it’s used to retrieve arrays, strings, etc.

Grammar:

new Set(val)
Copy the code

Set is a constructor. Val does not qualify any type, as long as it has an Iterator interface.

The following data has an Iterator interface

  • An array of
  • Map
  • Set
  • string
  • Class array, for exampleNodeList.arguments
var set = new Set('112233')
console.log(set) // Set(3) {'1', '2', '3'}

var set = new Set([1.2.3.1.2.3])
console.log(set) // Set(3) {1, 2, 3}
Copy the code

You can see that all the printed data is the deleted data.

var set = new Set(['1'.1.1])
console.log(set) // Set(3) {'1',1}

var set = new Set(/ {}, {})console.log(set) // Set(3) {{},{}}

var set = new Set([NaN.NaN])
console.log(set) // Set(3) {NaN}
Copy the code

Set does not convert, the number 1 is different from the string 1. And then the key values are different for the same object, but NaN is considered the same, so that’s something to be careful about.

Instance properties

Set.prototype.size

This returns the number of internal members of a set instance. It has no length attribute.

var set = new Set('112233')
console.log(set.size) / / 3
Copy the code

Method of instance

Set.prototype.add(value)

Add data to a set instance

var set = new Set([1.2.3])
set.add(4)
set.add(5)
console.log(set) // Set(5) {1, 2, 3, 4, 5}
Copy the code

Set.prototype.delete(value)

Delete data from a set instance. If the data does not exist in the instance, the deletion fails and the instance is not affected.

var set = new Set([1.2.3])
set.delete(3)
console.log(set) // Set(5) {1, 2}
Copy the code

Set.prototype.has(value)

Does the set instance have a value? Returns a Boolean value.

var set = new Set([1.2.3])
console.log(set.has(3)) // true
console.log(set.has(4)) // false
Copy the code

Set.prototype.clear()

Clears all data of the set instance

var set = new Set([1.2.3])
set.clear()
console.log(set) // Set(0) {size: 0}
Copy the code

keys(),values(), entries()

These are methods that iterate over a Set instance and return an Iterator. The Set has the same key and value, so keys() and values() return the same object.

And the Set instance itself has an Iterator interface, which can also do for-of traversal.

var set = new Set([1.2.3])
for(let key of set) {console.log(key)} / / 1 2 3
for(let key of set.keys()) {console.log(key)} / / 1 2 3
for(let key of set.values()) {console.log(key)} / / 1 2 3
for(let key of set.entries()) {console.log(key)} / / [1, 1] [2] [3, 3]
Copy the code

In addition to the above, there is a forEach method that traverses a Set instance

var set = new Set([1.2.3])
set.forEach((value,key) = > {consoe.log(value, key)}) // Value is the same as key
Copy the code

Extended operator

Because a set instance is an interface with Iterator, it can be converted to an array using the extension operator.

var set = new Set([1.2.3])
console.log([...set]) / / [1, 2, 3]
Copy the code

WeakSet

WeakSet is similar to Set, and the limiting value cannot be repeated, and the value must be the data of the interface with Iterator. But the value must be an object, not another type. For an array, each item in the array needs to be an object type.

new WeakSet([1.2.3]) // error: Invalid value used in weak set
new WeakSet([[1], [2], [3]]) // success
new WeakSet([{a:1}, {a:2}]) // success
Copy the code

Also, unlike Set, it is a weak reference, which is automatically collected by the garbage collection mechanism when idle, without manual cleanup.

Its instance has no size property, only add,delete, and HAS methods.

var weakset = new WeakSet(a)var arr = [[1]]
weakset.add(arr) // WeakSet {Array(1)}
weakset.size // undefined
weakset.has(arr) // true
weakset.delete(arr) // WeakSet {}
Copy the code

reference

Set and Map data structures