Javascript 用另一个变量引用一个变量值

Javascript 用另一个变量引用一个变量值,javascript,Javascript,我想将一些数字值存储为变量,然后通过组合其他变量来引用它们,但我不能将这些值插入内部html,只能插入变量名 var a1 = 2.00; var a2 = 4.00; var a3 = 6.00; var input1 = $("#one").val(); //gives a var input2 = $("#two").val(); //gives 1, 2 or 3 var code = input1 + input2; //combines input 1 and input 2 to

我想将一些数字值存储为变量,然后通过组合其他变量来引用它们,但我不能将这些值插入内部html,只能插入变量名

var a1 = 2.00;
var a2 = 4.00;
var a3 = 6.00;
var input1 = $("#one").val(); //gives a
var input2 = $("#two").val(); //gives 1, 2 or 3

var code = input1 + input2; //combines input 1 and input 2 to give eg a2
document.getElementById("output").innerHTML = code;

目前,我可以输出例如“a1”、“a2”、“a3”,但不能输出我分配给这些变量的值。我想我需要更改“.innerhtml=code;”上的语法,但我不确定如何更改。我还尝试将a变量设置为带有“price”的对象,但仍然存在相同的问题

将代码存储在对象中,而不是单独的变量中。然后使用从变量构造对象键:

var codes = {
  a1: 2.00,
  a2: 4.00,
  a3: 6.00
}

var input1 = $("#one").val(); // gives a
var input2 = $("#two").val(); // gives 1, 2 or 3

var code = codes[input1 + input2]; // combines input 1 and input 2 to give eg a2
document.getElementById("output").innerHTML = code;

您应该使用一个对象来存储值,您可以使用该对象从对象
obj

var obj={
a1:2.00,
a2:4.00,
a3:6.00
}
变量input1='a';
变量input2='2';
var代码=obj[input1+input2]//结合输入1和输入2,得出eg a2

控制台日志(代码)@PraveenKumar抱歉,我希望输出为2.00或4.00或6.00(基本上是价格值)