javascript - JS: add a variable as a property (.) with (+) -


if trying add variable property error:

"uncaught syntaxerror: unexpected token +"

so trying add variable loop property json this:

   var tables = ["table1", "table2", "table3"];    (var x = 0; x < tables.length; x++) {      var item = $database. + tables[x];      console.log(item);    } 

if use (") var item = "$database." + tables[x];

it works, becomes string (if that's proper name) can not view json objects.

why happening , possible do?

thanks!

try this:

for (var x = 0; x < tables.length; x++) {     var item = $database[tables[x]];     console.log(item); } 

if $database dynamic , don't want have hard-coded array of table names, can use object.keys():

var tables = object.keys($database); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -