当前位置:网站首页>Ts-Map 类的使用
Ts-Map 类的使用
2022-08-01 10:55:00 【小蜗牛游戏】
Map 相关的函数与属性:
- map.clear() – 移除 Map 对象的所有键/值对 。
- map.set() – 设置键值对,返回该 Map 对象。
- map.get() – 返回键对应的值,如果不存在,则返回 undefined。
- map.has() – 返回一个布尔值,用于判断 Map 中是否包含键对应的值。
- map.delete() – 删除 Map 中的元素,删除成功返回 true,失败返回 false。
- map.size – 返回 Map 对象键/值对的数量。
- map.keys() - 返回一个 Iterator 对象, 包含了 Map 对象中每个元素的键 。
- map.values() – 返回一个新的Iterator对象,包含了Map对象中每个元素的值 。
迭代 Map
Map 对象中的元素是按顺序插入的,我们可以迭代 Map 对象,每一次迭代返回 [key, value] 数组。
TypeScript使用 for...of 来实现迭代:
// map中的迭代
let keys:Iterable<string> = map.keys();
// 迭代map中的所有键
for(let key of map.keys()) {
console.log("keys",key);
}
// 迭代map中的所有值
for(let value of map.values()) {
console.log("values",value);
}
// 迭代map
for(let entry of map.entries()){
console.log("entries1",entry[0], entry[1]);
}
for(let [key,value] of map.entries()) {
console.log("entries2",key, value);
}
// 遍历map
map.forEach((value:number,key:string,map:Map<string,number>)=>{
console.log("forEach",key,value);
});
// 清空map
map.clear();
边栏推荐
猜你喜欢

Flutter Widget 如何启用和屏蔽点击事件

STM32 Personal Notes - Watchdog

Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (3) Background Functions

Endorsed in 2022 years inventory | product base, science and technology, guangzhou automobile group striding forward

SAP ABAP OData 服务如何支持 $orderby (排序)操作试读版

回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出

Promise learning (2) An article takes you to quickly understand the common APIs in Promise

Promise learning (4) The ultimate solution for asynchronous programming async + await: write asynchronous code in a synchronous way

Drawing arrows of WPF screenshot control (5) "Imitation WeChat"

Promise学习(二)一篇文章带你快速了解Promise中的常用API
随机推荐
Glassmorphism design style
【无标题】
退役划水
回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出
机器学习 | MATLAB实现支持向量机回归RegressionSVM参数设定
July 31, 2022 -- Take your first steps with C# -- Use arrays and foreach statements in C# to store and iterate through sequences of data
如何设计一个分布式 ID 发号器?
Golang内存分析工具gctrace和pprof实战
July 31, 2022 -- Take your first steps with C# -- Use C# to create readable code with conventions, spaces, and comments
Stone Technology builds hard-core brand power and continues to expand the global market
Introduction to data warehouse layering (real-time data warehouse architecture)
CTFshow,命令执行:web37
小程序毕设作品之微信美食菜谱小程序毕业设计成品(4)开题报告
我是如何保护 70000 ETH 并赢得 600 万漏洞赏金的
语音聊天app源码——语音聊天派对
.NET性能优化-使用SourceGenerator-Logger记录日志
JWT
gc的意义和触发条件
CTFshow,命令执行:web33
线上问题排查常用命令,总结太全了,建议收藏!!