ECMAScript6(ES6)之map数据结构

ECMAScript6(ES6)之map数据结构

1、介绍
      es6提供Map数据结构。它类似对象,也是键值对的集合,但是“键”的范围不限于字符串,各种类型的值(包括对象)都可以当键。也就是说,Object结构提供了“字符串一值”的对应。Map结构提供了“值一值”的对应。是一种更完善的Hash结构实现。建立“键值对”的数据结构,Map比Object更适合。

var m=new Map();
var o={p:"hello world"};
m.set(o,"content")
console.log(m.get(o))//content
console.log(m.has(o))//true
console.log(m.delete(o))//true
console.log(m.has(o))//false
    Map可以接受一个数组作为参数。该数组的成员是一个个表示键值对的数组
let map=new Map([['name',"张三"],["title","Author"]]);
console.log(map.size)//2
console.log(map.has("name"))//true
console.log(map.get("name"))//"张三"
     Map构造函数接受数组作为参数,实际上执行的是下面的算法。
let items=[['name',"张三"],["title","Author"]];
let map=new Map();
items.forEach(([key,value])=>map.set(key,value));
console.log(map.size)//2
console.log(map.has("name"))//true
console.log(map.get("name"))//"张三"
     注意:只有对同一个对象的引用,Map结构才将其视为同一个键,Map的键是和内存地址绑定的,主要内存地址不一样,就视为两个键。
    下面的set和get方法,表面上是针对同一个键。但实际上这是两个值,内存地址是不一样的。因此get方法无法读取该键,返回undefined,同理,同样的值的两个实例,在Map结构中被视为两个键。如变量k1和k2的值是一样的,但它们在Map结构中被视为两个键。。
let map=new Map();
map.set(['a'],5555);
console.log(map.get(['a']));//undefined
let k1=['a'];
let k2=['a'];
map.set(k1,222).set(k2,444);
console.log(map.get(k1),map.get(k2))//222 444
2、属性

  • size属性
let map =new Map();
map.set('foo',true).set("bar",false);
console.log(map.size)//2
  • set(key,value)
         set方法设置key所对应的键值,然后返回整个Map结构。如果key已经有值,则键值会被更新,否则新生成该键
let m=new Map();
m.set("edition",6)//键是字符串
m.set(262,"standard")//键是数字
m.set(undefined,"nah")//键是undefined
//set方法返回的是Map本身,因此可以采用链式写法。
let m=new Map();
m.set("edition",6).set(262,"standard").set(undefined,"nah")
  • get(key)
        get读取key对应的键值,如果找不到key,则返回undefined
let m =new Map();
m.set('foo',true).set("bar",false);
console.log(m.get('foo'),m.get(1))//true undefined
  • has(key)
         has方法返回一个布尔值,表示某个键是否在Map结构中
let m=new Map();
m.set("edition",6).set(262,"standard").set(undefined,"nah");
console.log(m.has("edition"),m.has(262),m.has(undefined),m.has(333))//true true true false
  • delete(key)
         delete方法删除某个键,返回true,如果删除失败,则返回false
let m =new Map();
m.set('foo',true).set("bar",false);
console.log(m.delete("bar"));//true
console.log(m.has("bar"));//false
  • clear()
         clear方法清除所有成员,没有返回值
let m =new Map();
m.set('foo',true).set("bar",false);
console.log(m.size)//2
m.clear();
console.log(m.size)//0
3、遍历方法
  • Map原生提供3个遍历器生成函数和1个遍历方法。
  • keys():返回键名的遍历器
  • values():返回键值的遍历器
  • entries():返回所有成员的遍历器
  • forEach():遍历Map的所有成员。
let map=new Map([['f','no'],['t','yes']]);
for(let key of map.keys()){
	console.log(key);//f ,t
}
for(let value of map.values()){
	console.log(value)//yes,no
}
for(let item of map.entries()){
	console.log(item[0],item[1]);//f no    t yes
}

for(let [key,value] of map.entries()){
	console.log(key,value) //f no    t yes
}
//Map默认的遍历接口是entries方法
for(let [key,value] of map){
	console.log(key,value) //f no    t yes
}
4、扩展运算符(...)
使用扩展运算符(...)让Map结构快速地转为数组结构比较
let map=new Map([['f','no'],['t','yes']]);
let x=[...map.keys()];
console.log(x); //["f", "t"]

let c=[...map.values()];
console.log(c); // ["no", "yes"]

let v=[...map.entries()];
console.log(v); // ["f", "no"]  ["t", "yes"]

let b=[...map];
console.log(b); // ["f", "no"]  ["t", "yes"
参考:ES6标准入门(第二版)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值