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]); } 

demo


Comments

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -