1. size 属性
size 属性返回 Map 结构的成员总数。
示例26
| const hzhMap = new Map ([ |
| ['name', '黄子涵'], |
| ['brother', '黄春钦'], |
| ['mother', '陈兰英'] |
| ]); |
| console.log("hzhMap成员总数:"); |
| console.log(hzhMap.size); |
| [Running] node "e:\HMV\JavaScript\JavaScript.js" |
| hzhMap成员总数: |
| 3 |
| |
| [Done] exited with code=0 in 0.173 seconds |
2. set 和 get 方战
set 方法设置键名 key 对应的键值为 value,然后返回整个 Map 结构。如果 key 已经有对应的键值,则键值会被更新,否则就新生成键值。则方法返回的是当前的 Map 对象,因此可以采用链式写法。
示例27
| const hzhMap = new Map ([ |
| ['name', '黄子涵'], |
| ['brother', '黄春钦'], |
| ['mother', '陈兰英'] |
| ]); |
| hzhMap.set('singer',['周杰伦', '蔡依林']).set('edition', 6); |
| console.log(hzhMap); |
| [Running] node "e:\HMV\JavaScript\JavaScript.js" |
| Map { |
| 'name' => '黄子涵', |
| 'brother' => '黄春钦', |
| 'mother' => '陈兰英', |
| 'singer' => [ '周杰伦', '蔡依林' ], |
| 'edition' => 6 |
| } |
| |
| [Done] exited with code=0 in 0.254 seconds |
get 方法读取 key 对应的键值,如果找不到 key,则返回 undefined 。
示例28
| const hzhMap = new Map ([ |
| ['name', '黄子涵'], |
| ['brother', '黄春钦'], |
| ['mother', '陈兰英'] |
| ]); |
| hzhMap.set('singer',['周杰伦', '蔡依林']).set('edition', 6); |
| console.log("使用get方法读取name的键值:"); |
| console.log(hzhMap.get('name')); |
| console.log(""); |
| console.log("使用get方法读取brother的键值:"); |
| console.log(hzhMap.get('brother')); |
| console.log(""); |
| console.log("使用get方法读取mother的键值:"); |
| console.log(hzhMap.get('mother')); |
| [Running] node "e:\HMV\JavaScript\JavaScript.js" |
| 使用get方法读取name的键值: |
| 黄子涵 |
| |
| 使用get方法读取brother的键值: |
| 黄春钦 |
| |
| 使用get方法读取mother的键值: |
| 陈兰英 |
| |
| [Done] exited with code=0 in 0.2 seconds |
3. has 方法
has 方法返回一个布尔值,判断某个键是否在当前 Map 对象之中。
示例 29
| const hzhMap = new Map ([ |
| ['name', '黄子涵'], |
| ['brother', '黄春钦'], |
| ['mother', '陈兰英'] |
| ]); |
| hzhMap.set('singer',['周杰伦', '蔡依林']).set('edition', 6); |
| console.log("判断name键是否在Map对象之中:"); |
| console.log(hzhMap.has('name')); |
| console.log(""); |
| console.log("判断brother键是否在Map对象之中:"); |
| console.log(hzhMap.has('brother')); |
| console.log(""); |
| console.log("判断mother键是否在Map对象之中:"); |
| console.log(hzhMap.has('mother')); |
| console.log(""); |
| console.log("判断edition键是否在Map对象之中:"); |
| console.log(hzhMap.has('edition')); |
| console.log(""); |
| console.log("判断father键是否在Map对象之中:"); |
| console.log(hzhMap.has('father')); |
| [Running] node "e:\HMV\JavaScript\JavaScript.js" |
| 判断name键是否在Map对象之中: |
| true |
| |
| 判断brother键是否在Map对象之中: |
| true |
| |
| 判断mother键是否在Map对象之中: |
| true |
| |
| 判断edition键是否在Map对象之中: |
| true |
| |
| 判断father键是否在Map对象之中: |
| false |
| |
| [Done] exited with code=0 in 0.339 seconds |
4. delete 方法
delete 方法删除某个键,如果删除成功,返回 true;如果删除失败,返回 false。如示例30如所示。
示例 30
| const hzhMap = new Map ([ |
| ['name', '黄子涵'], |
| ['brother', '黄春钦'], |
| ['mother', '陈兰英'] |
| ]); |
| hzhMap.set('singer',['周杰伦', '蔡依林']).set('edition', 6); |
| console.log("使用delete方法读取edition键:"); |
| console.log(hzhMap.delete('edition')); |
| console.log(""); |
| console.log("使用delete方法读取father的键值:"); |
| console.log(hzhMap.get('father')); |
| console.log(""); |
| console.log("打印hzhMap:"); |
| console.log(hzhMap); |
| [Running] node "e:\HMV\JavaScript\JavaScript.js" |
| 使用delete方法读取edition键: |
| true |
| |
| 使用delete方法读取father的键值: |
| undefined |
| |
| 打印hzhMap: |
| Map { |
| 'name' => '黄子涵', |
| 'brother' => '黄春钦', |
| 'mother' => '陈兰英', |
| 'singer' => [ '周杰伦', '蔡依林' ] |
| } |
| |
| [Done] exited with code=0 in 0.318 seconds |
5. 遍历方法
Map 结构原生提供 3 个遍历器生成函数和 1 个遍历方法。具体使用方法查看示例 31 。
keys():返回键名 的遍历器。
values():返回键值的遍历器。
entries():返回所有成员的遍历器 。
for Each():遍历 Map 的所有成员。
示例 31
| const hzhMap = new Map ([ |
| ['name', '黄子涵'], |
| ['brother', '黄春钦'], |
| ['mother', '陈兰英'] |
| ]); |
| console.log("遍历输出键名:"); |
| for(let key of hzhMap.keys()) { |
| console.log(key); |
| } |
| console.log(""); |
| console.log("遍历输出键值:"); |
| for(let value of hzhMap.values()) { |
| console.log(value); |
| } |
| console.log(""); |
| console.log("遍历输出键名和键值:"); |
| for(let [key, value] of hzhMap.entries()) { |
| console.log(key, value); |
| } |
| console.log(""); |
| console.log("遍历输出键名和键值:"); |
| hzhMap.forEach(function(value,index){ |
| console.log(index + ":" + value); |
| }) |
| [Running] node "e:\HMV\JavaScript\JavaScript.js" |
| 遍历输出键名: |
| name |
| brother |
| mother |
| |
| 遍历输出键值: |
| 黄子涵 |
| 黄春钦 |
| 陈兰英 |
| |
| 遍历输出键名和键值: |
| name 黄子涵 |
| brother 黄春钦 |
| mother 陈兰英 |
| |
| 遍历输出键名和键值: |
| name:黄子涵 |
| brother:黄春钦 |
| mother:陈兰英 |
| |
| [Done] exited with code=0 in 1.031 seconds |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!