Gallaxhar
Gallaxhar

Reputation: 1036

How to get nested value from a Map object?

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')); 

Upvotes: 2

Views: 6915

Answers (4)

DanG
DanG

Reputation: 91

I'm doing something like this to create graphs/trees right now. This isn't exactly what I'm doing but it should give you the idea.

In es5 I would say

let root = {
  1:  {
       a: 'hi from a'
       b: ...
       }  
  2:  {...}
}

root[1][a]; //returns 'hi from a'
root['getme']['abeer']; //Type Error 

using Map:

  let root = new Map();
  root.set(1, new Map());
  root.get(1).set('a', 'hi from a');

  root.get(1).get('a');  //returns 'hi from a'

  root.get(1).get('me a beer');  //undefined  

  root.get('me').get('a-beer'); 
  //Uncaught TypeError: Cannot read property 'get' of undefined

Upvotes: 0

Youssef BH
Youssef BH

Reputation: 582

You can try es6 destructuring 101.

let obj = {'k1': {'k2': {'k3': 'Test Value'}}};

let {k1} = obj;
let {k1: {k2}} = obj;
let {k1: {k2: {k3}}} = obj;

console.log(k1);
console.log(k2);
console.log(k3)

I hope this will help.

Upvotes: 0

joelnet
joelnet

Reputation: 14231

Your question about querying a nested object using Map is invalid. Map cannot do this. You either have to flatten your object prior to putting it into Map or find an alternative way that supports querying nested data.

I would advise against flattening your structure for the sole purpose of using Map. How do you handle key collisions? It becomes more complex.

So let's look at some ways to query nested data:

// how to query a nested data structure.

const get = (prop, ...props) => obj =>
  obj == null || prop == null
    ? obj
    : get(...props)(obj[prop])

const obj = {'k1': {'k2': {'k3': 'Test Value'}}};
const result = get('k1', 'k2', 'k3')(obj) // => 'Test Value'

You can also use a library like Ramda.

import path from 'ramda/src/path

const obj = {'k1': {'k2': {'k3': 'Test Value'}}};
const result = path(['k1', 'k2', 'k3'], obj)  // => 'Test Value'

Upvotes: 3

Nina Scholz
Nina Scholz

Reputation: 386550

You need to iterate all keys and the nested objects as well.

function add(object, map) {
    Object.entries(object).forEach(([k, v]) => {
        map.set(k, v);
        if (v && typeof v === 'object') {
            add(v, map);
        }
    });
}

var obj = { k1: { k2: { k3: 'Test Value' } } };
const map = new Map;

add(obj, map);

console.log(map.get('k1')); 
console.log(map.get('k2')); 
console.log(map.get('k3')); 

Upvotes: 2

Related Questions