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

MySQL index

keytool: command not found

反思 | Android 音视频缓存机制的系统性设计

SAP PI/PO Soap2Proxy 消费外部ws示例

SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)

配置npm

Implementation of MySQL persistence

ABAP 7.4 SQL Window Expression
![[Educational Codeforces Round 80] 解题报告](/img/54/2fd298ddce3cd3e28a8fe42b3b8a42.png)
[Educational Codeforces Round 80] 解题报告

数论分块(整除分块)
随机推荐
保洁阿姨都能看懂的中国剩余定理和扩展中国剩余定理
Visualization Road (IX) detailed explanation of arrow class
数论分块(整除分块)
Mysql隔离级别
14.事务处理
P2257 YY的GCD(莫比乌斯反演)
9.常用函数
7.子查询
Mysql持久性的实现
1. View databases and tables
[牛客挑战赛47]C.条件 (bitset加速floyd)
Applet Wx Previewmedia related problem solving - Daily stepping on the pit
4.多表查询
图论入门——建图
学会使用搜索引擎
数据库查询优化的方式
BTree、B+Tree和HASH索引
Authorization server (simple construction of authorization server)
安装配置淘宝镜像npm(cnpm)
7. sub query