当前位置:网站首页>认识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()边栏推荐
- Stream data
- C IO stream (II) extension class_ Packer
- 【TSP问题】基于Hopfield神经网络求解旅行商问题附Matlab代码
- Apache foundation officially announced Apache inlong as a top-level project
- Redisson 3.17.4 release
- Compile the telegraph desktop side (tdesktop) using vs2022
- How to bypass SSL authentication
- SVN
- jarvisoj_level2_x64
- Redisson 3.17.4 发布
猜你喜欢

Apache基金会正式宣布Apache InLong成为顶级项目

Middle order clue binary tree

每日刷题记录 (四)

机器视觉:照亮“智”造新“视”界

Drag the mouse to rotate the display around an object

1-9Vmware中网络配置

Machine vision: illuminating "intelligence" and creating a new "vision" world

Wireshark's analysis of IMAP packet capturing

Penetration tool -burpsuite

渗透工具-Burpsuite
随机推荐
idea设置mapper映射文件的模板
DBCA silent installation and database building
Ssl/tls, symmetric and asymmetric encryption, and tlsv1.3
Servlet response download file
【TSP问题】基于Hopfield神经网络求解旅行商问题附Matlab代码
Compile the telegraph desktop side (tdesktop) using vs2022
Maintenance and key points of SMT Mounter
信号处理函数内必须使用可重入函数
When installing PSU /usr/bin/ld:warning: -z lazload ignore
Law and self-regulation in the meta universe
Why is it best to use equals for integer comparisons
1-9network configuration in VMWare
Precautions for cleaning PCBA board in SMT chip processing
基于OpenVINOTM开发套件“无缝”部署PaddleNLP模型
Explanation of chip processing manufacturer__ What is ICT? What is the main test? Advantages and disadvantages of ICT testing?
What do SMT operators do? operating duty?
Leetcode 513. Find the value in the lower left corner of the tree
Phoenix index
Explain from a process perspective what happens to the browser after entering a URL?
jarvisoj_level2_x64