tags:

views:

109

answers:

2

In the following statement, how could I make the "log.entries[i]" part a variable?

json.log.entries[i]

eval works but I don’t want to use it.

eval("json" + "var")

I also tried:

json[var]

Thanks

A: 

You would have to split it up like this:

json["log"]["entries"][i];

But honestly I think that eval really would be your best option for a situation like this. Why do you need to store this as a string? Perhaps if I knew more about the problem you are trying to solve I could give you a btter solution.

Andrew Hare
I don't think eval is ever a good choice, at least for application code. Caja, Cajita, ADsafe, FBJS, and Jacaranda all prevent use of eval. There's always another way. Unfortunately, I can't make out what the questioner is trying to do yet, so I can't offer a solution.
Nosredna
A: 

It sounds like the target variable you're trying to refer to is "jsonvariablename", and not a property of an object called "json" which could be referred json.variablename / json[variablename]. This is not a good naming structure for variables and eval is indeed required in this case -- is it possible for you to change the variable format so that the variables are properties of an object called JSON? That would be the best solution.

Ben
I think json[variablename][variablename] would be what he is looking for. As far as I understand, he wants to address a variable property of a variable object. Is it possible to address object members that way in JS? Would be news to me but then, I'm no JS guru.
Pekka
Yeah, I couldn't tell for sure from question whether he's looking for PREFIXrestofvariablename or object.property. If eval("json" + "var") works that suggests the former.
Ben