How to create a map in TypeScript

1 Answer

0 votes
let mp = new Map<string, number>([["typescript", 7], ["javascript", 8], ["nodejs", 5], ["c", 2]])

console.log(mp)

console.log();

console.log([...mp.entries()]);

console.log();

mp.forEach((value: number, key: string) => {
    console.log(key, value);
});

console.log();

for (let [key, value] of mp) {
    console.log(key, value);
} 
 
 
 
   
/*
run:
        
Map (4) {"typescript" => 7, "javascript" => 8, "nodejs" => 5, "c" => 2} 

[["typescript", 7], ["javascript", 8], ["nodejs", 5], ["c", 2]] 

"typescript",  7 
"javascript",  8 
"nodejs",  5 
"c",  2 

"typescript",  7 
"javascript",  8 
"nodejs",  5 
"c",  2 
           
*/
 

 



answered Aug 13, 2022 by avibootz
edited Aug 13, 2022 by avibootz

Related questions

...