0

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
Yun
  • 23
  • 6
  • 1
    use `get()`: `newMap.get('something')` – 0stone0 Nov 01 '22 at 13:42
  • Your confusion is because the question you've linked to is regarding the `map()` method of an **Array**, not a `Map` object. For details on the latter, here's an MDN reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map. As @0stone0 points out, you need to use `get()` to retrieve the value from a `Map` – Rory McCrossan Nov 01 '22 at 13:45

2 Answers2

1

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);
Volodymyr Sichka
  • 531
  • 4
  • 10
1
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);
});
dtm
  • 191
  • 6