How to iterate over a map in reverse order in Node.js

2 Answers

0 votes
let mp = new Map();
   
mp.set("NodeJS", 23);
mp.set("Java", 89);
mp.set("C", 31);
mp.set("C++", 93);
mp.set("PHP", 77);

const arr = Array.from(mp).reverse();

arr.forEach(([key, value]) => {
  	console.log(key, value); 
});
   
  
   
        
        
/*
run:
        
PHP 77
C++ 93
C 31
Java 89
NodeJS 23
        
*/

 



answered May 16, 2022 by avibootz
0 votes
let mp = new Map();
   
mp.set("NodeJS", 23);
mp.set("Java", 89);
mp.set("C", 31);
mp.set("C++", 93);
mp.set("PHP", 77);

Array.from(mp)
   .reverse()
   .forEach(element => {
          console.log(element, element[0], element[1]); 
   });
   
  
   
        
        
/*
run:
        
[ 'PHP', 77 ] PHP 77
[ 'C++', 93 ] C++ 93
[ 'C', 31 ] C 31
[ 'Java', 89 ] Java 89
[ 'NodeJS', 23 ] NodeJS 23
        
*/

 



answered May 16, 2022 by avibootz

Related questions

1 answer 141 views
1 answer 156 views
3 answers 245 views
245 views asked Jan 20, 2022 by avibootz
2 answers 162 views
2 answers 149 views
1 answer 108 views
...