javascript - Have a variable select from an array -
this question has answer here:
i trying use variable select array:
this works:
var myarray = {bricks:3000, studs:500, shingles:400, tiles:700}; function one() { alert(myarray.bricks); }
but not work:
var myarray = {bricks:3000, studs:500, shingles:400, tiles:700}; var myvalue = "bricks" function two() { alert(myarray.myvalue); }
how do properly? here fiddle show trying accomplish: https://jsfiddle.net/chrislascelles/xhmx7hgc/2/
use []
notation.
var myarray = {bricks:3000, studs:500, shingles:400, tiles:700}; function one() { alert(myarray.bricks); } var myvalue = "bricks" //supplied here make example work function two() { alert(myarray[myvalue]); }
Comments
Post a Comment