当前位置:网站首页>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
边栏推荐
猜你喜欢
SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)
ABAP 7.4 SQL Window Expression
学习笔记6-几种深度学习卷积神经网络的总结
SAP TRANSLATE使用数据对象掩码示例
学习笔记5-梯度爆炸和梯度消失(K折交叉验证)
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
Javscript gets the real suffix of the file
Authorization+Token+JWT
菜菜的并发编程笔记 |(九)异步IO实现并发爬虫加速
SAP PI/PO Soap2Proxy 消费外部ws示例
随机推荐
组合数求解与(扩展)卢卡斯定理
技能点挖坑
[LNOI2014]LCA——树链剖分——多点LCA深度和问题
MySQL isolation level
学习笔记7-深度神经网络优化
‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
Applet newline character \ nfailure problem resolution - Daily pit stepping
13.用户和权限管理
[hdu6833]A Very Easy Math Problem(莫比乌斯反演)
简易随机点名抽奖(js下编写)
manjaro安装与配置(vscode,微信,美化,输入法)
1D/1D动态规划学习总结
SAP PI/PO登录使用及基本功能简介
keytool: command not found
Django使用mysql数据库报错解决
F-牛妹的苹果树(直径合并)
6. Aggregation function and grouping statistics
VR、AR、MR的区别与应用,以及对AR技术的一些实现原理
13. User and authority management
ABAP 7.4 SQL Window Expression