当前位置:网站首页>认识map
认识map
2022-06-25 22:40:00 【ivanfor666】
/* * Map * * * */
let myMap = new Map();
let keyObj = {};
myMap.set(keyObj, "和键keyObj关联的值");
myMap.size;
// 1 myMap.get(keyObj);
// "和键keyObj关联的值" myMap.get({});
// undefined, 因为keyObj !== {}
// 遍历Map
for (var [key, value] of myMap) {
console.log(key + " = " + value);
}
for (var key of myMap.keys()) {
console.log(key);
}
for (let value of myMap.values()) {
console.log(value);
}
Array.from(myMap); // [[key,value]] Array.from(myMap.keys())
// 复制或合并 Maps
let one = new Map([[1, 'one'], [2, 'two'], [3, 'three'],]);
new Map(one);
// 浅克隆
let first = new Map([[1, 'one'], [2, 'two'], [3, 'three'],]);
let second = new Map([[1, 'uno'], [2, 'dos']]);
// 合并两个Map对象时,如果有重复的键值,则后面的会覆盖前面的。
// 展开运算符本质上是将Map对象转换成数组。
let merged = new Map([...first, ...second]);
console.log(merged.get(1));
// uno console.log(merged.get(2));
// dos console.log(merged.get(3)); // three
// 请注意!为Map设置对象属性也是可以的,但是可能引起大量的混乱。
let wrongMap = new Map()
wrongMap['bla'] = 'blaa'
wrongMap['bla2'] = 'blaaa2'
console.log(wrongMap) // Map { bla: 'blaa', bla2: 'blaaa2' }
// Map 中的 key 是有序的。因此,当迭代的时候,一个 Map 对象以插入的顺序返回键值。
// 利用Map 去重
var map = new Map;
[1, 1, 22, 33, 22, 33, 11, 1].forEach(item => map.set(item, true));
map.keys()
边栏推荐
- 渲云携手英特尔,共创云渲染“芯”时代
- C IO stream (II) extension class_ Packer
- No executorfactory found to execute the application
- Drag the mouse to rotate the display around an object
- What are the red lines of open source that should not be trodden on?
- Logstash discards log data that does not match the file name exactly
- Apache基金会正式宣布Apache InLong成为顶级项目
- How to bypass SSL authentication
- [OEM special event] in the summer of "core cleaning", there are prize papers
- Example: use C # Net to teach you how to develop wechat official account (21) -- using wechat to pay online collection: H5 method
猜你喜欢
【超能云终端创领先机】如何在48小时内交付一座方舱医院?
Regular expression introduction and some syntax
Ad20 (Altium designer) PCB highlight network
Idea view unit test coverage
debezium
Ora-01153: incompatible media recovery activated
Penetration tool -burpsuite
鼠标拖拽围绕某个物体旋转展示
把控元宇宙产业的发展脉络
Permission design = function permission + Data permission
随机推荐
The development context of Ba Kong Yuan universe industry
Installation and configuration of gradle environment
11.1.2 overview of Flink_ Wordcount case
Precautions for cleaning PCBA board in SMT chip processing
Mining pit record of modified field information in Dameng database
Some basic uses of mongodb
MySQL custom function instance
Installing redis on Linux
debezium
CXF
SQL to retain the maximum value sorted by a field
每日刷题记录 (四)
【TSP问题】基于Hopfield神经网络求解旅行商问题附Matlab代码
Wireshark's analysis of IMAP packet capturing
Camkiia et gcamp6f sont - ils les mêmes?
Redux workflow + complete code of small examples
学习识别对话式问答中的后续问题
. user. PHP website installation problems caused by INI files
性能领跑云原生数据库市场!英特尔携腾讯共建云上技术生态
Data synchronization