Reputation: 43
I have the following (snippet) variable:
var txt = {
'start': {
'name':'Call start',
'data': {'next':'start/e3fe40'},
'id':'start',
'type':'standard---start'
},
'e3fe40': {
'name':'Menu',
'data': {'next':'end/asd3rg'},
'id':'e3fe40'
}
};
I need to parse through the JSON and get info from the 'e3fe40' branch (without knowing how its going to be called.
Here's what I have:
var nxt = txt.start.data.next.substr(6,10); <-- works
console.log(nxt); <-- works
console.log(txt.start.data.next); <-- works
console.log(txt.nxt.name); <-- nxt should contain 'e3fe40'
So, how do I go down a branch? txt.nxt.name won't work, txt.{nxt}.name won't work, etc....
Thanks, Dan
Upvotes: 1
Views: 120
Reputation: 5186
If You debug code by using debugging tool and put this code I think it will solve your problem
alert(txt.e3fe40.name);
Upvotes: 0
Reputation: 8411
Assuming nxt = "e3fe40"
you get the value by doing.
txt[nxt].name
Different ways of accessing the values,
txt.e3fe40.name
txt['e3fe40'].name
and one shown above.
txt.nxt.name
is wrong. Because nxt is not a key in the Javascript object,
Upvotes: 0
Reputation: 25165
var key = txt.start.data.next.substr(6);
console.log(txt[key].name);
Upvotes: 2