-1

I have a multidimensional array in javascript, jsfiddel please check the below code. Some thing like this working in php but not in javascript, is this possible in javascript.

<script>
    var mydata = {'test' : 'testing'};
    var mydata2 = {'test2' : mydata};
    var myvar = 'test2';
    // This one is working
    alert(mydata2.test2.test);
    // but this is not working
    alert(mydata2.myvar.test);
</script>  
Noor
  • 45
  • 8

1 Answers1

0

You need the bracket notation for this:

 alert(mydata2[myvar].test);
 //           ^     ^

Bracket notation

get = object[property_name];
object[property_name] = set;

property_name is a string. The string does not have to be a valid identifier; it can have any value, e.g. "1foo", "!bar!", or even " " (a space).

var mydata = { 'test': 'testing' },
    mydata2 = { 'test2': mydata },
    myvar = 'test2';

document.write(mydata2.test2.test + '<br>');
document.write(mydata2[myvar].test);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392