Instead of using object literal syntax to access nested values in objects, I'm trying to use es6 Map object, which has a convenient map.get() method. I'm trying to avoid having to do the following with normal Javascript objects.
// access deeply nested values...
obj['k1'] &&
obj['k1']['k2'] &&
obj['k1']['k2']['k3']
Am I building the map wrong? only map.get('k1') works (can't get nested map values, just the whole thing under k1: is given as a value).
var obj = {'k1': {'k2': {'k3': 'Test Value'}}};
const map = new Map(Object.entries(obj));
console.log(map.get('k1'));
console.log(map.get('k2'));
console.log(map.get('k3'));
k1, in the new map. A map by itself won't solve your problem; you still need to traverse the structure to drill down.k1,Object.entries(obj)will give only one key/value pair, so the resulting map will also only have that one.getso you could do something like.getml('k1.k2.k3')... but internally it's going to have to access each level in turn similarly to what you're trying to avoid. A better question might be: If you're not using those top levels of your data structure, why do you have them?Map.