ES6 系列六:set和map

快来加入我们吧!

"小和山的菜鸟们",为前端开发者提供技术相关资讯以及系列基础文章。为更好的用户体验,请您移至我们官网小和山的菜鸟们 进行学习,及时获取最新文章。

"Code tailor" ,如果您对我们文章感兴趣、或是想提一些建议,微信关注 “小和山的菜鸟们” 公众号,与我们取的联系,您也可以在微信上观看我们的文章。每一个建议或是赞同都是对我们极大的鼓励!

前言

在开始学习之前,我们想要告诉您的是,本文章是对阮一峰《ECMAScript6 入门》一书中 “Set 和 Map” 章节的总结,如果您已掌握下面知识事项,则可跳过此环节直接进入题目练习

  • 什么是 Set 和 Map?
  • Set 和 Map 的常用方法

如果您对某些部分有些遗忘,👇🏻 已经为您准备好了!

学习链接

set 和 map 的学习

汇总总结

概念

Set 对象是值的集合,你可以按照插入的顺序迭代它的元素。 Set中的元素只会出现一次,即 Set 中的元素是唯一的。

Map 对象保存键值对,并且能够记住键的原始插入顺序。任何值(对象或者原始值) 都可以作为一个键或一个值。

set 和 map 的常用方法

Set

  • Set()

创建一个新的 Set 对象。

let mySet = new Set()
  • Set.prototype.size

返回 Set 对象中的值的个数

let mySet = new Set()

mySet.add(1) // Set [ 1 ]
mySet.add(5) // Set [ 1, 5 ]
mySet.size // 2
  • Set.prototype.add(value)

Set 对象尾部添加一个元素。返回该 Set 对象

let mySet = new Set()
mySet.add(1) // Set [ 1 ]
mySet.add(5) // Set [ 1, 5 ]
  • Set.prototype.clear()

移除 Set 对象内的所有元素。

let mySet = new Set()
mySet.add(1) // Set [ 1 ]
mySet.add(5) // Set [ 1, 5 ]
mySet.clear()
mySet.size // 0
  • Set.prototype.delete(value)

移除 Set 中与这个值相等的元素,返回 Set.prototype.has(value) 在这个操作前会返回的值(即如果该元素存在,返回 true,否则返回 false)。Set.prototype.has(value) 在此后会返回 false

let mySet = new Set()
mySet.add(1) // Set [ 1 ]
mySet.add(5) // Set [ 1, 5 ]
mySet.delete(5) // true,  从set中移除5
mySet.size // 1
  • Set.prototype.has(value)

返回一个布尔值,表示该值在 Set 中存在与否。

let mySet = new Set()

mySet.add(1) // Set [ 1 ]
mySet.add(5) // Set [ 1, 5 ]

mySet.has(1) // true
mySet.has(3) // false

mySet.delete(5) // true,  从set中移除5
mySet.has(5) // false, 5已经被移除

mySet.size // 1, 刚刚移除一个值

Map()

  • Map()

用于创建 Map 对象

let myMap = new Map()
  • Map.prototype.size

返回 Map 对象的键/值对的数量。

let myMap = new Map()
let keyString = 'a string'
myMap.set(keyString, "和键'a string'关联的值")
myMap.size //1
  • Map.prototype.clear()

移除 Map 对象的所有键/值对 。

myMap.clear()
  • Map.prototype.delete(key)

如果 Map 对象中存在该元素,则移除它并返回 true;否则如果该元素不存在则返回 false。随后调用 Map.prototype.has(key) 将返回 false

let keyString = 'a string'
myMap.set(keyString, "和键'a string'关联的值")
myMap.delete('a string') //true
myMap.delete('xhs') //false(因为没有set)
  • Map.prototype.get(key)

返回键对应的值,如果不存在,则返回 undefined

myMap.get(keyString) // "和键'a string'关联的值"
  • Map.prototype.has(key)

返回一个布尔值,表示 Map 实例是否包含键对应的值。

myMap.has('a string') // true
  • Map.prototype.set(key, value)

设置 Map 对象中键的值。返回该 Map 对象。

myMap.set(keyString, "和键'a string'关联的值")
let myMap = new Map()
let keyString = 'a string'
myMap.set(keyString, "和键'a string'关联的值")
myMap.size //1
myMap.get(keyString) // "和键'a string'关联的值"
myMap.has('a string') // true
myMap.delete('a string') //true
myMap.clear()

题目自测

一:Map 和 Set 的区别


二:求数组的并集与交集

题目解析

一、

Answer:

Set 对象是值的集合,Map 对象是键值对的集合 Set 对象的元素只会出现一次,即 Set 中的元素是唯一的。Map 对象由于如果有重复的键值,则后面的会覆盖前面的,相对来说键也是唯一的

二、

Answer:

const array1 = [1, 3, 5, 7, 9]
const array2 = [2, 3, 4, 5, 6]

const s1 = new Set([...array1, ...array2]) //并集
let Union = [...s1]
console.log(Union) // [1, 3, 5, 7, 9, 2, 4, 6]

const s2 = new Set(array1) //交集
const Intersection = array2.filter((item) => s2.has(item))
console.log(Intersection) // [3, 5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值