Home > Software design >  how do I read values inside the nested map using nodejs or javascript
how do I read values inside the nested map using nodejs or javascript

Time:11-02

I am very new in javascript and currently trying to read the value newMap but don't know, who do I read the values of Map?

const map1 = new Map();

map1.set('usagesUnkown', 10);
map1.set('usagesUnkown2', 10);

const newMap = new Map();
newMap.set("anoop",map1);

I have already try these answer text but nothing is working.

CodePudding user response:

Check this:

const map1 = new Map();

map1.set('usagesUnkown', 10);
map1.set('usagesUnkown2', 10);

const newMap = new Map();
newMap.set('anoop', map1);

const nestedValue = newMap.get('anoop').get('usagesUnkown');
console.log(nestedValue);

CodePudding user response:

const map1 = new Map();

map1.set('usagesUnkown', 10);
map1.set('usagesUnkown2', 10);

const newMap = new Map();
newMap.set("anoop",map1);

const refToMap1 = newMap.get("anoop"); //return map 1
const usagesUnkown = refToMap1.get("usagesUnkown"); //return 10

//loop through a map
refToMap1 .forEach((value, key) => {
  console.log(value, key);
});
  • Related