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?

5
  • Would it be var myValue = theObject['keyName10']['keyName3'] ? Commented Mar 6, 2013 at 22:48
  • Yes, it would @Bertrand. That is not what the question is all about. Commented Mar 6, 2013 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. Commented Mar 6, 2013 at 23:05
  • 1
    You can easily convert this to a recursive function, but a loop is the simplest approach IMHO. Commented Mar 6, 2013 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: benalman.com/news/2010/03/theres-no-such-thing-as-a-json. Commented Mar 6, 2013 at 23:27

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Not bad. I might do something like this.
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) 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.