当前位置:网站首页>Use of typescript dictionary
Use of typescript dictionary
2022-04-23 07:42:00 【0zien0】
because typescript Similar information is not provided directly map Key value pair data structure for us to use , So we can use some special ways to achieve the purpose we need :
1. establish :
let dic : {[key:string]:number} = {};
Use the above statement to create a key of type string, The type of value is number Dictionary
2. add to :
dic[“zien”] = 23;
dic.clz = 13;
You can add key values in both ways
3. Delete :
delete dic[“zien”];
4. lookup :
for(let key in dic){
let value = dic[key]
}
The routine addition, deletion, modification and check are roughly the same , The above is basically typescript The original method , Through simple application to achieve similar Dictionaries The use effect of .
If you want to realize the dictionary function you want more dynamically , You can encapsulate a class according to your project needs to realize the functions you want , Such as :
export default class Dictionary {
items: object;
constructor() {
this.items = {
};
}
has(key: any): boolean {
return this.items.hasOwnProperty(key);
}
set(key: any, val: any) {
this.items[key] = val;
}
delete(key: any): boolean {
if (this.has(key)) {
delete this.items[key];
}
return false;
}
get(key: any): any {
return this.has(key) ? this.items[key] : undefined;
}
values(): any[] {
let values: any[] = [];
for (let k in this.items) {
if (this.has(k)) {
values.push(this.items[k]);
}
}
return values;
}
}
Reference material :
https://www.jianshu.com/p/48281bfff687
版权声明
本文为[0zien0]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230623293779.html
边栏推荐
猜你喜欢
h5本地存储数据sessionStorage、localStorage
Discussion on arrow function of ES6
快速下载vscode的方法
SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)
菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案
Background management system framework, there is always what you want
SAP PI/PO Soap2Proxy 消费外部ws示例
manjaro安装与配置(vscode,微信,美化,输入法)
页面实时显示当前时间
Mysql 索引
随机推荐
OpenGL超级宝典初步配置(freeglut、glew、gltools、glut)
经典套路:一类字符串计数的DP问题
8.分页查询
快速傅里叶变换FFT简明教程
Mysql 索引
Date对象(js内置对象)
keytool: command not found
莫比乌斯反演
typescript字典的使用
SAP ECC连接SAP PI系统配置
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
Mysql隔离级别
11.表和库的管理
Educational Codeforces Round 81 (Rated for Div. 2)
组合数求解与(扩展)卢卡斯定理
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
[hdu6868]Absolute Math(推式子+莫比乌斯反演)
9.常用函数
青龙面板拉库命令更新【2022/4/20】收藏不走丢
3. Sort statement