当前位置:网站首页>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
边栏推荐
猜你喜欢

页面动态显示时间(升级版)

自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)

Mysql持久性的实现

BTree、B+Tree和HASH索引

Solutions to common problems in visualization (VII) solutions to drawing scale setting

SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)

Mysql 索引

Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process

Discussion on arrow function of ES6

反思|开启B站少女心模式,探究APP换肤机制的设计与实现
随机推荐
手游的热更方案与动态更新策略
h5本地存储数据sessionStorage、localStorage
Mysql 索引
[COCI] Vještica (子集dp)
13.用户和权限管理
[Ted series] how to get along with inner critics?
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
王者荣耀-unity学习之旅
数据分析入门 | kaggle泰坦尼克任务(三)—>探索数据分析
莫比乌斯反演
Nacos/sentinel网关限流和分组 (代码)
ABAP 实现发布RESTful服务供外部调用示例
游戏辅助脚本开发之旅
每日一题 | 曾被反转链表支配的恐惧
积性函数前缀和——杜教筛
对STL容器的理解
Mysql隔离级别
SAP PI/PO登录使用及基本功能简介
数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理
配置npm