ES6 Set And Map

Set and Map are two new data structures for collection provided by ES6. Although other traditional programming languages like java, php, have provide Set and Map from the beginning, EMCAScript also tend to absorb their advantages and implement more features that itself didn’t provide.

Set

Set is a kind of collections whose containing elements are unique, which means that if adding the same elements into a set, it will just store one and neglect the others. The criteria for equality in a set is almost the same to strict equality ===, except that NaN is considering to equal to itself, which is different from ===.

// create a new Set
let set = new Set()
// create a new Set from array or set
let set1 = new Set([1, 2, 1])
let set2 = new Set(set1)
console.log(set1)
// 1 2


// Set provides property .size and methods .add(), .delete(), .has() and .clear() for element operations
let set = new Set()
set.add(1) // you could just add one element at a time
set.add(1) // do nothing because the set contains the element
set.has(1) // return true if the set containing the element
set.add(2)
set.delete(1) // delete the element, it will do nothing if the element does not exist
set.clear() // clear all elements in the set


// Set provides methods .keys() .values(), .entries() and .forEach() for iteration. These methods return a iterator, which you could not use .add() or .delete() methods.
// Since Set does not have key, so .keys(), .values() are the same. .entries() return iterator that contains elements in set as key and value in an array
let set = new Set([0, 1, 2])
for (let key of set.keys()) {
    console.log(key)
}
for (let value of set.values()) {
    console.log(value)
}
for (let entry of set.entries()) {
    console.log(entry)
}
set.forEach((value) => {
    console.log(value)
})
// 0 1 2
// 0 1 2
// [0, 0] [1, 1] [2, 2]
// 0 1 2

// default iterator is .values()
for (let value of set) {
    console.log(value)
}
// 0 1 2

Map

Map is a kind of kay-value collection. It provides hash method to directly mapping from key to memory address, so it’s efficient for data look-up. In ES5, we implement “mapping” function with object, but that has a drawback: the key in Object could just be String. If the key is Number or Object, it will be converted to String with toString(). In ES6, any data type could be a key in map. Map could anything to a memory address for storage.

// create a new Map
let map = new Map()
// create a new Map with an array
let map1 = new Map([['key1', 'value1'], ['key2', 'value2']])
// Map has .size property and .get(), .set(), .delete(), .has(), .clear() methods for operations
map.set('name', 'name')
// .set() return the map itself, so chain rule is applicable
map.set('key1', 'value1').set('key2', 'value2')
map.get('name') // 'name', if not exist, return undefined
map.has('name') // true
map.delete('name') // delete the key and return true, if the key does not exist, do nothing and return false
map.clear() // reset the map


// Map provides .keys(), .values(), .entries() and .forEach() for iteration
let map = new Map([['key1', 'value1'], ['key2', 'value2']])
for (let key of map.keys()) {
    console.log(key)
}
// key1 key2
for (let value of map.values()) {
    console.log(value)
}
// value1 value2
for (let entry of map.entries()) {
    console.log(entry[0], entry[1])
}
// key1, value1
// key2, value2
// the same as to
for (let [key, value] of map.entries()) {
    console.log(key, value)
}
// the same as to
for (let [key, value] of map) {
    console.log(key, value)
}

WeakSet And WeakMap

WeakSet is a special kind of Set whose element must be object, and the object it contains could be destroyed if no other reference(except the one in WeakSet).
WeakMap is similar to Map too, except that its keys must be object and also the object it refer to might be destroyed if no other reference.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: ES6中的SetMap是两种新增的集合类型。Set是一种无重复值的集合,可以通过new Set()来创建。它具有add方法用于向集合中添加元素,has方法用于判断集合中是否存在某个元素,clear方法用于清空集合。Set也可以用于数组去重,通过new Set(\[...\])的方式将数组转换为Set,利用Set的特性去除重复值。\[1\] Map是一种键值对的集合,可以通过new Map()来创建。它具有set方法用于向集合中添加键值对,get方法用于获取指定键名对应的值,has方法用于判断集合中是否存在某个键名,clear方法用于清空集合。Map的键名可以是任意数据类型,包括引用值,但需要注意的是,引用值作为键名时,需要使用相同的引用地址才能获取到对应的值。\[2\] SetMap都可以使用for...of循环或forEach方法进行遍历。在Set中,forEach的第二个参数是集合的元素,因为Set中不存在下标。而在Map中,forEach的第一个参数是键值对的值,第二个参数是键值对的键名。\[3\] 总结来说,SetMapES6中新增的集合类型,Set用于存储无重复值的集合,Map用于存储键值对的集合。它们提供了一些方法来操作集合,如添加、获取、判断是否存在等。在使用时需要注意它们的特性和方法的使用方式。 #### 引用[.reference_title] - *1* *2* [ES6中的MapSet详解](https://blog.csdn.net/m0_45093055/article/details/126430467)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ES6中的setmap](https://blog.csdn.net/weixin_44247866/article/details/127561391)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值