一、ES6新增数据结构(set/map)
(1)set/map概念
两个都是es6新增的构造函数:
- set:类似于数组,只能存值没有键,值是唯一的
- map:类似于对象,可以存键和值,键名不限于字符串,键和值一一对应
set、map、Promise、Proxy四个无法通过babel编译语法降级(poly feel、babel poly feel)
(2)set/map深入
-
set
-
原型 var set = new Set();
console.log(set);
-
参数 参数是具备 iterator 接口的数据结构:[],类数组 var set = new Set([5,7]);
console.log(set);
-
声明方式
var set = new Set([1,2,3]);
console.log(set);
var set = new Set();
set.add(1).add(2).add(3);
console.log(set);
-
值是唯一的 var set = new Set([1,2,3,4,5,5,5,5]);
console.log(set);
-
特殊值 var set = new Set([undefined,undefined,null,null,5,'5',true,1,NaN,NaN,{},{},[],[]])
console.log(set);
-
操作方法
-
add 追加数据,返回值是set实例 var set = new Set();
var x = {id:1};
var y = {id:2};
set.add(x);
set.add(y);
set.add(x)
.add(y)
.add(x);
console.log(set);
-
size 返回当前长度 console.log(set.size);
-
delete 清除某个值,返回值是布尔值,操作是实时的 console.log(set.delete(y));
console.log(set);
-
clear 清空所有的值,返回值是undefined,操作是实时的
console.log(set);
set.clear()
console.log(set);
-
has 判断是否有指定值,返回值是布尔值 console.log(set.has(x));
-
遍历方法
-
keys/values/entries 遍历键,值,键值对数组,返回值是迭代器对象 set 不存在键名,故键名和键值是一致的 let set = new Set([1, 2, 3, 4, 5, 6, 7]);
console.log(set.keys());
console.log(set.values());
console.log(set.entries());
for (let i of set.keys()) {
console.log(i);
}
for (let i of set.values()) {
console.log(i);
}
for (let i of set.entries()) {
console.log(i);
}
-
迭代器 set对象上也部署了迭代器,可通过for…of遍历
console.log(Set.prototype[Symbol.iterator] === Set.prototype.values);
for(let values of set){
console.log(values);
}
-
forEach 遍历成员(键、值、set结构) let set = new Set(['a', 'b', 'c', 'd']);
set.forEach((value, keys, s) => {
console.log(value, keys, s)
})
-
set经常和…,map,filter搭配使用
-
拓展运算符 —— 去重 去重最简单的方式就是收集到set当中,再展开到一个数组里
let set = new Set(['a','b','c','d','e','f','g']);
console.log(...set);
let set = new Set(['a','b','c','d','e','f','g'];
console.log([...set]);
-
map —— 操作成员 let set = new Set([1,2,3,4,5,6,7])
let set1 = new Set([...set].map(value => value*2));
console.log(set1);
难点 var arr = [1,2,3,4]
var arr1 = arr.map(parseInt);
console.log(arr1);
var arr1 = arr.map((value,idx)=> console.log(value,idx));
parseInt(1,0)parseInt(2,1)parseInt(3,2)parseInt(4,3);
let set2 = new Set([[...set].map(parseInt)]);
console.log(set2);
-
filter —— 过滤成员 let set2 = new Set([...set].filter(x => (x%2) == 0))
console.log(set2);
-
交集并集差集 let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
let union = new Set([...a, ...b]);
console.log(union);
let intersect = new Set([...a].filter(x => b.has(x)));
console.log(intersect);
let difference = new Set([...a].filter(x => !b.has(x)));
console.log(difference)
-
映射新的结构
let set = new Set([1,2,3]);
let set1 = new Set([...set].map(value => value*2));
let set = new Set([1,2,3]);
let set1 = new Set(Array.from(set,value=>value*2));
-
map
-
原型 和set的原型基本一致,只是多了set、get方法,主要用于存取键值,而set本身没有键,所以不需要这两个方法 -
与对象对比
-
普通对象 键和值不能实现一一对应 var m = {}
var x = {id:1},
y = {id:2};
m[x] = "foo";
m[y] = "bar";
console.log(m);
console.log(m[x]);
console.log(m[y]);
-
map结构 键和值一一对应 let m = new Map();
var x = {id:1},
y = {id:2};
m.set(x,'foo');
m.set(y,'bar');
console.log(m.get(x));
console.log(m.get(y));
-
参数
- 参数是具备 iterator 接口的数据结构:[],类数组
- 参数内部是双元数据结构
[['键名', '键值'], ...] let m = new Map([
['name', 'zhangsan'],
['title', 'lisi']
])
console.log(m)
-
声明方式
let m = new Map([
['name', 'wangwu'],
['title', 'zhaoliu']
])
var items = [['name','wagwu'],['title','zhaoliu']]
let m = new Map();
items.forEach(([key,value]) => m.set(key,value))
let m = new Map();
map.set('name','zhangsan')
map.set('title','lisi')
-
覆盖问题
-
原始值 const map = new Map();
map.set(1, 'foo');
map.set(1, 'bar');
map.set(-0,123);
console.log(map.get(+0))
map.set(true,1);
map.set('true',2);
console.log(true);
map.set(undefined,1)
map.set(null,2)
log(map.get(undefined));
log(map.get(null));
map.set(NaN,123);
log(map.get(NaN));
-
引用值
map.set([5],555);
console.log(map.get([5]));
var arr = [5];
map.set(arr,555);
console.log(map.get(arr));
原始值在栈里面存储的是地址,引用值在栈里面存储的是指向堆的指针
-
操作方法
- set
添加成员,返回值set实例本身const m = new Map();
m.set(1,"foo").set(2,"bar");
console.log(m);
- get
获取成员,返回值set实例本身m.get(1);
- size
获取成员长度var x = {id:1},
y = {id:2};
m.set(x,"foo");
m.set(y,"bar") ;
cosnole.log(m.size);
- delete
删除成员,返回值是布尔值,操作是实时的var x = {id:1},
y = {id:2};
m.set(x,"foo");
m.set(y,"bar") ;
cosnole.log(m.delete(x));
m.delete(x);
- clear
清空成员,返回值undefined,操作是实时的var x = {id:1},
y = {id:2};
m.set(x,"foo");
m.set(y,"bar") ;
cosnole.log(m.clear());
m.clear();
- has
判断成员,返回值是布尔值var x = {id:1},
y = {id:2};
m.set(x,"foo");
m.set(y,"bar") ;
cosnole.log(m.has(x));
-
遍历方法
- keys/values/entries
var x = {id:1},
y = {id:2}
m.set(x,"foo")
m.set(y,"bar")
for(let keys of m.keys()){
console.log(keys)
}
for(let keys of m.values()){
console.log(values)
}
for(let keys of m.entries()){
console.log(entries)
}
- 迭代器
console.log(m[Symbol.iterator] === m.entries)
for(let i of m){
console.log(i)
}
for(let [key,values] of m){
console.log(key,values)
}
|