'How to get a value from map in a map?

I have a map where there are three values for every key, and can print them. However, I don't know how to reference each individual value.

const planinfo = await statement.planInformation(pool);
//  console.log(planinfo);
  let planinfoMap = new Map()
  planinfo.forEach(value=> {
    planinfoMap.set(value.plansubdomain, new Map([
      ['plancode',value.plancode],
      ['urlbucket',value.urlbucket],
      ['planemail',value.planemail]
      ]))
  })
// get subdomain here
console.log(planinfoMap.get(subdomain));

Map(3) {
  'plancode' => '222',
  'urlbucket' => 'somes3bcuket',
  'planemail' => '[email protected]'
}

Map(3) {
  'plancode' => '111',
  'urlbucket' => 'thiss3bucket',
  'planemail' => '[email protected]'
}

How do I reference plancode, urlbucket, and planemail individually?



Solution 1:[1]

You need to access the object's properties. For an exhaustive explanation read the doc on Object and the doc on Map.

let oneMapObject = planinfoMap.get(subdomain);
console.log("You mapped: %s, %s, %s", oneMapObject.get("plancode"), oneMapObject.get("urlbucket"), oneMapObject.get("planemail"));
console.log("This mapObject has plancode: %s", oneMapObject.plancode);
console.log("Another way to look at it: %s & %s (urlbucket and planemail)", oneMapObject["urlbucket"], oneMapObject["planemail"] );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lee Taylor