Set a new data structure ES6 in understanding and usage details description

1, the data structure described Set

ES6 provides a new data structure Set. It is similar to an array, but the value of the member is unique, no duplicate values.

Set itself is a constructor to generate Set data structure.

 

typeof Set

// function

typepf ( new Set()) 

// object

new Set() instanceof Set

// true

Set  instanceof Function

// true

 

 

 

2, the basic structure of a data type usage Set

 

const set   = new Set()

const array = [ 1, 3, 3, 2, 4, 2, 5, 0, 1, 5, 0 ,4]

array.forEach((x)=> { set.add(x)})

console.log(set)

 

the console.log (... SET) 

// Output 2 3 4 5 0 1 

the console.log (Array.from (SET)) 

// output [1, 3, 2, 4, 5, 0]

As can be seen from the above features, Set is an internal element of an array of structures will not be repeated, with its own size property has its own method for ... of traversal ... Alternatively, use of this feature, it is easy deduplication operation will be arrays or strings

const array = [ 1, 2, 3, 4, 4, 3 ,2, 1, 0]

const set = new Set(array)

const arr = Array.from(set)


const arr2 = [...set]

// 字符串去重

const string = 'abcddcba'

const str = [...new Set(string)].join('')

Notes: Set in a data structure that NaN and NaN are repeated, but in fact the JS NaN == NaN, other types of data is not strictly follow any two can be used ===! 

2, the properties and methods Set constructor

Set above the constructor prototype constructor property has Set.prototype.constructor === Set

Set above prototype constructor has size attribute, which indicates the number of members of Set, note that the class is used in the array ArrayLike length property

Set four constructors above operation method, respectively,

Add members: Set.prototype.add (value), remove members: Set.prototype.delete (value), whether it has:  Set.prototype.has (value), empty:  Set.prototype.clear (),  

 

const array = [1, 2, 3, 4, 5, 6]

const set = new Set(array)

set.add(7) 

console.log(set.has(7))

// true

set.delete(7)

console.log(set.has(7))

// false

set.clear()

console.log(...set)

// undefined

 

There are four methods to traverse the prototype constructor Set

Get all keys: Set.prototype.keys (), get all values: Set.prototype.values ​​(), to obtain an array of key names and values: Set.prototype.entries (), traversing Set: Set.prototype.forEach

 

const array = ['Apple', 'Google', 'Ali', 'Baidu', 'Tecent']

const set = new Set(array)

console.log(set.keys())

console.log(set.values())

console.log(set.entries())

 

Can be known from the figure, the three traversal method, are returned to traverse the object Set of the visitor object for ... of loop

for(let item of set.keys()) {
    console.log(item)
}

// 输出 Apple Google Ali  Baidu  Tecent

for(let item of set.values()) {
    console.log(item)
}

// 输出 Apple Google Ali  Baidu  Tecent


for(let item of set.entries()) {
    console.log(item)
}

// 输出 ['Apple', 'Apple']  ['Google', 'Google'] ['Ali', 'Ali'] ['Baidu', 'Baidu'] ['Tecent', 'Tecent']

Set the prototype there is a constructor method forEach, for each member of Set to do something with no return value

const set = new Set([1, 2, 3])

set.forEach((item)=>{
    console.log(item)
})

// 1 2 3

 

3、WeakSet 

 

Set WeakSet similar structure, it is not a duplicate set of values. However, it has two differences with the Set.

 

First of all, it can only be a member of WeakSet objects, but can not be other types of values.

 

Secondly, WeakSet the objects are weak references, that garbage collection is not considered WeakSet reference to the object, that is, if the object is no longer referenced by the other objects, garbage collection automatically reclaims the object occupied memory, regardless of the object also exists in WeakSet.

 

Because of the above features, the members WeakSet is not suitable reference, because it will disappear at any time. In addition, due to internal WeakSet the number of members, depending on the garbage collection mechanism has not run before and after the run is likely the number of members is not the same, and when to run the garbage collection mechanism is unpredictable, so ES6 provisions WeakSet can not traverse.

 

WeakSet can not traverse, because members are weak references, could disappear at any time, there is no guarantee that the members of traversing mechanism, it may have just traversed the end, we did not get the members. One use WeakSet is stored DOM nodes, but do not worry about these nodes removed from the document, it will lead to memory leaks.

 

Guess you like

Origin www.cnblogs.com/liquanjiang/p/11431944.html