Set
Features:
Es6 new Set([1,2,3]);
Properties:
size
: Gets the number of elements
Methods:
add(value)
: Adds the element and returns the Set instance itselfdelete(value)
: Deletes an element and returns the result of the deletion (Boolean)has(value)
: Indicates whether the Set instance has this element and returns the query resultclear()
: Clears all elements with no return value
const setOb = new Set([1.2])
setOb.add(2).add(3)
console.log(setOb) / / {1, 2, 3}
console.log(setOb.size) / / 3
console.log(setOb.has(1)) // true
console.log(setOb.has(3)) // true
console.log(setOb.has(4)) // false
setOb.delete(2)
console.log(setOb) / / {1, 3}
console.log(setOb.has(2)) // false
setOb.clear()
console.log(setOb) // Set(0) {size: 0}
Copy the code
Iterate over the Set instance
keys()
: returns a traverser for key namesvalues()
: iterator that returns key values. Keys () and values() return the same valueentries()
: returns a traverser for key-value pairsforEach()
: Iterates through each element using the callback function
const setOb = new Set([Patrick's Star.Spongebob Squarepants.Squidward])
console.log(setOb.keys()) // SetIterator {' Patrick ', 'SpongeBob ',' Squidbrother '}
for (let item of setOb.keys()) {
console.log(item)
}
/ / sent great stars
// SpongeBob
/ / the octopus
console.log(setOb.values()) // SetIterator {' Patrick ', 'SpongeBob ',' Squidbrother '}
for (let item of setOb.values()) {
console.log(item)
}
/ / sent great stars
// SpongeBob
/ / the octopus
console.log(setOb.entries()) / / SetIterator {' sent great stars' = > 'sent great stars',' spongebob squarepants' = > 'spongebob squarepants',' the octopus' = > 'octopus'}
for (let item of setOb.entries()) {
console.log(item)
}
// [' Patrick ', 'Patrick ']
// [' SpongeBob squarepants ', 'SpongeBob Squarepants ']
// [' Squidward ', 'Squidward ']
// Normally forEach cannot iterate over objects directly
console.log(setOb) // Set(3) {' Patrick ', 'SpongeBob ',' Squidbrother '}
setOb.forEach((value, key) = > {
console.log('key:' + key + ' value:' + value)
})
// key: padstar value: Padstar
// key: spongeBob value: SpongeBob
// key: squidward value: squidward
Copy the code
Map
Features:
Es6 adds, returns an initialization instance, and the argument can be an array or other iterable (null is treated as undefine)
Properties:
size
: Gets the number of elements
Methods:
set(key, value)
: Sets the key and value elementsget(key)
: Gets the value of the element through the element keyhas(key)
: Determines whether an element exists by the element keydelete(key)
: Deletes the corresponding element by keyclear()
: Clears all elements
const map = new Map(a)console.log(map) // Map(0) {size: 0}
map.set('pdx'.Patrick's Star)
console.log(map) //
map.set('hmbb'.Spongebob Squarepants)
console.log(map) // Map(1) {' PDX '=>' PDX '}
console.log(map.size) / / 2
console.log(map.get('pdx')) / / sent great stars
console.log(map.has('pdx')) // true
map.delete('pdx')
console.log(map.has('pdx')) // false
map.clear()
console.log(map) // Map(0) {size: 0}
Copy the code
Traverse the Map instance
keys()
: returns a traverser for key namesvalues()
: returns a traverser for key valuesentries()
: returns a traverser for all elementsforEach()
: Iterates through each element using the callback function
const map = new Map([['pdx'.Patrick's Star], ['hmbb'.Spongebob Squarepants]])
console.log(map) // Map(2) {' PDX '=>' PDX ', 'HMBB' => 'SpongeBob '}
map.set('zyg'.Squidward)
console.log(map) / / {' symbol '= >' sent great stars', 'HMBB' = > 'spongebob squarepants',' zyg '= >' octopus'}
console.log(map.size) / / 3
for (let key of map.keys()) {
console.log(key)
}
// pdx
// hmbb
// zyg
for (let value of map.values()) {
console.log(value)
}
/ / sent great stars
// SpongeBob
/ / the octopus
for (let item of map.entries()) {
console.log(item[0], item[1])}// PDX star
// SpongeBob squarepants
// Zyg
map.forEach((value, key) = > {
console.log(key, value)
})
// PDX star
// SpongeBob squarepants
// Zyg
Copy the code