当前位置:   article > 正文

ES6数据结构Set和Map的区别_es6数据结构中set和map有什么区别

es6数据结构中set和map有什么区别

参考1:https://www.cnblogs.com/lilife/p/13797853.html
参考2:https://es6.ruanyifeng.com/#docs/set-map
在这里插入图片描述

set 与 map 的数据结构

Set含义和基本用法

新数据结构Set。类似于数组,但是成员的值都是唯一的。所以可以用于数组去重。

const s = new Set();

[2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x));

for (let i of s) {
  console.log(i);
}
// 2 3 5 4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

两个空对象 所代表的内存地址是不一样的 所以可以看成两个值

let set = new Set();

set.add({});
set.size // 1

set.add({});
set.size // 2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
实例操作方法
Set.prototype.constructor:构造函数,默认就是Set函数。
Set.prototype.size:返回Set实例的成员总数。
Set.prototype.add(value):添加某个值,返回 Set 结构本身。
Set.prototype.delete(value):删除某个值,返回一个布尔值,表示删除是否成功。
Set.prototype.has(value):返回一个布尔值,表示该值是否为Set的成员。
Set.prototype.clear():清除所有成员,没有返回值。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
s.add(1).add(2).add(2);
// 注意2被加入了两次

s.size // 2

s.has(1) // true
s.has(2) // true
s.has(3) // false

s.delete(2);
s.has(2) // false
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
Map含义和基本用法

JavaScript对象object本质上是键值对的集合,Hash结构。

缺陷是只能使用字符串作为键。

而map结构优化了这个缺陷,它提供了值-值对的形式,让键名不再局限于字符串,是一种更完整的Hash结构实现

Map

可以接受一个数组作为参数,或者任何具有Iterator接口且每个成员都是一个双元素数组的数据结构都可以当作Map的构造函数的参数。

//set对象和map对象都可以以构造函数的形式生成一个新的map结构
const map =new Map()
const map =new Set()
//数组作为参数
const map =new Map([['name','rowland'],['type','1']])
  • 1
  • 2
  • 3
  • 4
  • 5
实例的属性和操作方法
Map.prototype.constructor:构造函数,默认就是Map函数。
Map.prototype.size:返回Map实例的成员总数。
Map.prototype.set(key, value):添加某个值,返回 Map结构本身。
Map.prototype.get(key):get方法读取key对应的键值,如果找不到key,返回undefined。 
Map.prototype.delete(value):删除某个值,返回一个布尔值,表示删除是否成功。
Map.prototype.has(value):返回一个布尔值,表示该值是否为Map的成员。
Map.prototype.clear():清除所有成员,没有返回值。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
const map = new Map();
map.set('foo', true);
map.set('bar', false);

map.size // 2

const m = new Map();

m.set('edition', 6)        // 键是字符串
m.set(262, 'standard')     // 键是数值
m.set(undefined, 'nah')    // 键是 undefined

let map = new Map()
  .set(1, 'a')
  .set(2, 'b')
  .set(3, 'c');
  
const m = new Map();

const hello = function() {console.log('hello');};
m.set(hello, 'Hello ES6!') // 键是函数

m.get(hello)  // Hello ES6!

const m = new Map();

m.set('edition', 6);
m.set(262, 'standard');
m.set(undefined, 'nah');

m.has('edition')     // true
m.has('years')       // false
m.has(262)           // true
m.has(undefined)     // true

const m = new Map();
m.set(undefined, 'nah');
m.has(undefined)     // true

m.delete(undefined)
m.has(undefined)       // false

let map = new Map();
map.set('foo', true);
map.set('bar', false);

map.size // 2
map.clear()
map.size // 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

由于map的键值可以是任意值,而对象所表示的内存地址。所以表面上是操作的一个map对象,实际上不是同一个。

const map = new Map()

map.set(['a'],111)

map.get(['a']) //underfined
  • 1
  • 2
  • 3
  • 4
  • 5

Map和Set的区别

  1. Map保存键值对,任何值都可以作为键值的值。
  2. Set保存的是类数组数据,是值的集合。
  3. Map有get方法而Set没有get方法
  4. map以键值对的形式存储,key=value组成pair,是一组映射关系。set只有值,可以认为只有一个数据,并且set中元素不可以重复且自动排序。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/990676
推荐阅读
相关标签
  

闽ICP备14008679号