See what the focus of today Set
Set in my mind it is mainly to heavy, Set
is a set of values, this collection of all the values appear only once
Set property size: Map and of the same size, the total number of members returned
Set method:
Set.prototype.add(value)
: Adding a value to return to Set structure itself.Set.prototype.delete(value)
: Delete a value, it returns a Boolean value that indicates whether the deletion was successful.Set.prototype.has(value)
: Returns a Boolean value that indicates whether the value isSet
a member.Set.prototype.clear()
: Clear all members, no return value.
let set = new Set(); set.add(1);set.add(1);set.add(2); set.has(1);//true set.has(2);//true set.add({x:2}); set.add({x:1}); set.add(function(){}); set.add([]); set.has(2)//true console.log(set.size) set.delete(1); set.forEach(point =>{ if(point.x<5){ set.delete(point) } }) console.log(set.size)//3 set.clear(); console.log(set.size)//0
Set inside a lot of the same Map and methods (including below). Likewise objects, functions, arrays, is unique address,
Set comprising determination has special value
+0 -0 is constant, and there undefned undefined, They is not repeated, and NaN and NaN are not identical, Set They think that equal
Set traversal methods:
Set.prototype.keys()
: Returns walker keys.Set.prototype.values()
: Returns the key value walker.Set.prototype.entries()
: Returns all members of the walker.Set.prototype.forEach()
: Through all the members of Set.
const set = new Set(['a', 'b', 'c']) for (let item of set.keys()) { console.log(item) } // a // b // c for (let item of set.values()) { console.log(item) } // a // b // c for (let item of set.entries()) { console.log(item) } // ["a", "a"] // ["b", "b"] // ["c", "c"] fordirectly set traversal example, is equivalent to traversing the set values of instance method// (let i of set) { console.log(i) } // a // b // c set.forEach((value, key) => console.log(key + ' : ' + value)) // a: a // b: b // c: c
Finally compare the difference
Set mainly to heavy, the only value,
Map predominantly present in the form of key-value pairs, similar to json, but contains all data types