0

I have an object like:

var theObject = { 
    keyName1: { keyName2: value2, keyName3: value3, keyName4: value40 },
    ...,
    keyName10: { keyName2: value6, keyName3: value7, keyName4: value8 }
}

I know I can reference value7 by theObject["keyName10"]["keyName3"] or theObject.keyName10.keyName3 but what I need is to set a variable to something like the search path and somehow pass it to theObject and get value7 directly.

Something like:

var path = keyName10.keyName3;
var myValue = theObject(path);

Objects can be even further into the object inception. Right now I'm solving it by horrible looking nestled for-loops. Is there a better way I missed?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
GusOst
  • 4,105
  • 3
  • 19
  • 28
  • 1
    possible duplicate of [Accessing nested JavaScript objects with string key](http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key) – Quentin Mar 06 '13 at 22:48
  • Would it be var myValue = theObject['keyName10']['keyName3'] ? – Bertrand Mar 06 '13 at 22:48
  • Yes, it would @Bertrand. That is not what the question is all about. – GusOst Mar 06 '13 at 23:03
  • @Quentin the suggested post uses tangled ugly (as in poor readability) for-loop. Similar to what I'm doing now. Looking for elegant and/or simple. – GusOst Mar 06 '13 at 23:05
  • 1
    You can easily convert this to a recursive function, but a loop is the simplest approach IMHO. – Felix Kling Mar 06 '13 at 23:07
  • And btw, I removed the `json` tag because your question has nothing to do with JSON. It's about how to access JavaScript objects. And no, they are not the same: http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/. – Felix Kling Mar 06 '13 at 23:27

2 Answers2

1

I just try to add a solution for fun. I will definitively do not use it like this, but the idea might work for your situation. I also question the efficiency of this approach.

var theObject = { 
    keyName1: { keyName2: value2, keyName3: value3, keyName4: value40 },
    ...,
    keyName10: { keyName2: value6, keyName3: value7, keyName4: value8 }
}
var path = 'keyName10/keyName3';


function getProp(theObject, path){
    var parts = path.split("/"),
    idx = parts[0],
    newParts = parts.splice(0, 1),
    newPath = newParts.join("/"),
    obj = theObject[idx];

    // add some validation and error handling in case or error on path 
    // or missing property on obj


    // I do not like the line below, would need to find a better way to see
    // if the function return something or it does some recursion
    // need to figure the right condition to see if we are at des
    if(parts.length == 1) {
        return obj;
    } else {
       return getProp(obj, newPath);
    }
}

Might help: How do I check if an object has a property in JavaScript?

Community
  • 1
  • 1
Bertrand
  • 388
  • 4
  • 13
0

Why not create a getter function...

var path = 'keyName10/keyName3'

function getTheThing(key){
    var parts = key.split("/")
    return theObject[parts[0]][parts[1]]
}

var myValue = getTheThing(path)

You could make it more general, by passing the object, and the key to the getter, allowing the path to be used to access different objects...

var path = 'keyName10/keyName3'

function getTheThing(key, obj){
    var parts = key.split("/")
    return obj[parts[0]][parts[1]]
}

var myValue = getTheThing(path,theObject)
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • For two this is good. Deeper in the Object inception/russian doll/nested object structure it will break. Will need to be able to pass in 'keyName10/keyName3/newKey/anotherKey/lotsOfKeys' as my real data, which I can't post, is extensive. – GusOst Mar 06 '13 at 22:58