Map : 集合

Map : 集合,
1. 如何创建map对象?
let map = new Map([
[1,2],
[‘1’,3],
[2,4],
[3,5],
[4,6]
]);
2. 属性:
size : 长度,

3. 方法:
	1. set() : 给map对象添加元素,
	let map = new Map();
	map.set(1,1).set(2,2).set(3,3).set(4,4).set(5,5);
	console.log(map);
	2. get() : 获取map对象中指定key的value,
		let map = new Map();
		map.set(1,1).set(2,2).set(3,3).set(4,4).set(5,5);
		console.log(map);
		console.log(map.get(3));
	2. delete() : 删除map对象中指定的元素,
	3. has() : 判断当前map中是否有指定的元素,返回布尔值,
	4. clear() : 清空map对象
4. 如何遍历map对象
	1. for(变量名 of map){}
	2. keys() : 获取集合中所有的key,
		for(let key of map.keys()){
			console.log("keys:" + key);
		}
	3. values() : 获取集合中所有的value,
		for(let value of map.values()){
			console.log('values:' + value);
		}
	4. entries() : 获取集合中所有的key和value,
		for(let [key,value] of map.entries()){
			console.log(key + '=>' + value);
		}
	5. forEach(function(value,key,map){})
		set.forEach(function(value,key,map){
			console.log(key,value,map);
		})

猜你喜欢

转载自blog.csdn.net/weixin_45052104/article/details/90932571
今日推荐