How to use map in JavaScript

2 Answers

0 votes
function print(mp) {
  for (let [key, value] of mp) {
      console.log(key, value);            
  }
}
 
let mp = new Map();
  
mp.set("TypeScript", 23);
mp.set("Java", 89);
mp.set("C", 31);
mp.set("C++", 93);
  
print(mp);
console.log();

console.log(mp.get("TypeScript"));      
console.log();

console.log(mp.has("Java"));        
console.log(mp.has("Python"));      
console.log(); 

console.log(mp.size);               
console.log();

mp.delete("TypeScript");    
print(mp);
console.log();

mp.clear();             
print(mp);

  
       
       
/*
run:
       
TypeScript 23
Java 89
C 31
C++ 93

23

true
false

4

Java 89
C 31
C++ 93
       
*/

 



answered Oct 10, 2019 by avibootz
edited May 6, 2024 by avibootz
0 votes
let map = new Map();  
 
map.set(1, "javascript");    
map.set(5, "php");    
map.set(6, "c#");    
map.set(9, "python");    
 
console.log(map.get(1));    
console.log(map.get(5));    

 
  
/*
run:
     
javascript
php
     
*/

 



answered May 6, 2024 by avibootz
...