Set和Map数据结构
Set是一种新的数据结构,类似于数组,但是成员的值都是唯一的,没有重复的值。Set本身是一个构造函数,用来生成Set数据结构。
Set可以接受具有iterable接口的其他数据结构作为参数,进行初始化。
var set=new Set([1,2,3,4,2,1,2]);
[...set];//1,2,3,4
set.size;//5
function divs(){
return [...document.querySelectorAll('div')];
}
var set=new Set(divs());
1
2
3
4
5
6
7
向Set中加入值时不会发生类型转换,**所以5和“5”是不同的值,Set内部判断相等用的是===,区别在于两个NaN认为是相等的。**不过两个相同的对象认为是不等的。
console.log([...new Set([NaN,NaN])]);//[NaN]
var set=new Set();
set.add({});
set.add({});
console.log(set.size);//2
1
2
3
4
5
Set的操作方法:
add(value):添加某个值,返回Set结构本身
delete(value):删除某个值,返回一个布尔值,表示删除是否成功
has(value):返回一个布尔值,表示参加是否为Set的成员。
clear():清除所有成员,没有返回值
var set=new Set();
set.add(1);
set.add(2);
set.has(1);//true
set.delete(1);
console.log(set.size);//1
set.clear();
console.log(set.size);//0
1
2
3
4
5
6
7
8
Array.from可以将Set结构转化为数组
//数组去重
function dedupe(array){
return Array.from(new Set(array));
}
dedupe([1,2,3,4,3,2]);//[1,2,3,4]
1
2
3
4
5
Set的遍历操作:
keys():返回键名的遍历器。
values():返回键值的遍历器。
entries():返回键值对的遍历器。
foreach():使用回调函数遍历每个对象。
var set=new Set(["a","b","c"]);
for(let i of set.entries()){
console.log(i);
}
// ["a", "a"]
//["b", "b"]
//["c", "c"]
for(let i of set.keys()){
console.log(i);
}
//a
//b
//c
for(let i of set.values()){
console.log(i);
}
//a
//b
//c
let set=new Set([1,2,3]);
set.forEach((value,key)=>console.log(value*2));
//2
//4
//6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
数组的map和filter方法也可以用于Set,还可以实现两个数组的
var set=new Set([1,2,3]);
set1=new Set([...set].map(x=>x*2));
console.log(set1);//Set(3) {2, 4, 6}
set2=new Set([...set].filter(x=>(x%2)==0);
console.log(set2);//Set(1) {2}
let a=new Set([3,5,6,8]);
let b=new Set([4,6,7,9]);
//并集
console.log(new Set([...a, ...b]));//set {3,5,6,8,4,7,9}
//交集
console.log(new Set([...a].filter(x=>b.has(x))));//set {6}
//差集
console.log(new Set([...a].filter(x=>!b.has(x))));//set {3,5,8}
let set=new Set([1,2,3]);
console.log(new Set(Array.from(set,val=>val*2)));//set的值是2,4,6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Map数据结构类似于对象,也是键值对的集合,但是对象的键只能是字符串,而Map的键可以是任意类型的值。
var m=new Map();
var o={p:'hello world'};
m.set(o,'value');
m.get(o);//'value'
m.delete(o);
m.has(o);//false
const map=new Map([
['name','远方'],
['age','12']
]);
console.log(map.size);//2
console.log(map.get('name'));//'远方'
console.log(map.has('age'));//true
//如果同一个键多次赋值,后面的值将覆盖前面的值,如果去读取一个未知的键,则返回undefined
const map=new Map();
map.set(1,'aaa').set(1,'bbb');
map.get(1);//aaa
map.get(2);//undefined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
只有对一个对象的引用,Map结构才将其视为同一个键。
注意[‘a’]和[‘a’]不是一个键,字符串’1’和数字1不是,undefined和null不是,NaN和NaN是一个,0和+0也是一个键。
Map结构的实例的属性
size
set(key,value)
get(key)
has(key)
delete(key)
clear()
var map=new Map();
map.set(1,'a').set(2,'b');
console.log(map.size);//2
console.log(map.get(1));//1
map.delete(1);
console.log(map.size);//1
map.clear();
console.log(map.size);//0
1
2
3
4
5
6
7
8
Map的遍历方法
keys()
values()
entries()
forEach()
const map=new Map([
[1,'a'],
[2,'b'],
[3,'c']
]);
console.log([...map.entries()]);//[[1,'a'],[2,'b'],[3,'c']]
console.log([...map]);//[[1,'a'],[2,'b'],[3,'c']]
console.log([...map.keys()]);//["a", "b", "c"]
console.log([...map.values()]);//["a", "b", "c"]
//结合数组的map方法、filter方法,可以实现Map的遍历和过滤(Map本身没有map和filter方法)。
const map1=new Map([...map].filter([k,v]=>k<3));//产生Map结构{1=>'a',2=>'b'}
const map2=new Map([...map].map((k,v)=>[k*2,'_'+v]));//产生Map结构{2=>'_a',4
————————————————
版权声明:本文为CSDN博主「@77」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41805715/article/details/84845080