- Commit
- Declare class with map
- Put and get then print
export class TestMap {
private map: Map<string, string> = new Map();
put(key:string, val:string): void {
if (key) {
this.map.set(key, val);
}
}
get(key:string): string | undefined {
if (key) {
return this.map.get(key)
}
}
count(): number {
return this.map.size;
}
clearByKeyPrefix(prefix: string) {
this.map.forEach((v: string, k:string, m:Map<string,string>) => {
if (k.startsWith(prefix)) {
m.delete(k);
}
})
}
}
const testMap = new TestMap();
testMap.put("ka", "vka");
testMap.put("kb", "vka");
testMap.put("kc", "vka");
testMap.put("kd", "vka");
testMap.put("ke", "vka");
testMap.put("aa", "vaa");
console.log(testMap);
console.log(testMap.count()); // 6
testMap.clearByKeyPrefix("k"); // print 6 entries
console.log(testMap.get("aa")); // print vaa