JavaScript访问动态创建的数组对象

时间:2017-05-11 11:24:12

标签: javascript arrays

我动态创建了一个数组列表,其语法如下:

<script>
    var item1001 = new Array();
    item1001[0] = 1001; //id
    item1001[1] = "Item name";
    item1001[2] = 500; //item price
    item1001[3] = "http://whatever"; //item page link

    var item1002 = new Array();
    item1002[0] = 1002; //id
    item1002[1] = "Item name";
    item1002[2] = 600; //item price
    item1002[3] = "http://whatever"; //item page link

    var item1003 = new Array();
    item1003[0] = 1003; //id
    item1003[1] = "Item name";
    item1003[2] = 700; //item price
    item1003[3] = "http://whatever"; //item page link
...
</script>

现在我有一个填充了所有项目的SELECT的表单:

<select name="items" id="items">
<option value="1001">Item name</option>
<option value="1002">Item name</option>
<option value="1003">Item name</option>
...
</select>

只是想在选择更改时检索商品价格,以便使用JavaScript进行一些计算:

jQuery( "#items" ).change(function() {          
    var myItemPrice="item"+Number(jQuery( "#items" ).val()+"[2]");
    console.log("Item price: "+myItemPrice);
    var total = Number(myItemPrice - (myItemPrice*5)/100);
    console.log("Total: "+total);

});

但我不知道如何访问阵列,即&#34; item1001 [2]&#34; dinamically,基于&#34; select&#34;值...

4 个答案:

答案 0 :(得分:1)

改为使用单个javascript对象:

var items = {};
items["1001"] = {name: "...", price: 100};
items["1005"] = {name: "...", price: 500};
var price = items[$('#items').val()].price;
..

答案 1 :(得分:1)

您可以使用window访问全局范围。

window['item' + $(this).val()][2]

但更好地重组您的初始数据。您可以使用对象来存储数据

var items = {
   1001: { id: 1001, name: '', price: 100},
   1002: { id: 1002, name: '', price: 100}
}


var price = items[$(this).val()].price

答案 2 :(得分:1)

只是为了展示如何将变量作为窗口的属性进行访问:

var item1001 =[];
item1001[0] = 1001; //id
item1001[1] = "Item name";
item1001[2] = 500; //item price
item1001[3] = "http://whatever"; //item page link

var item1002 = [];
item1002[0] = 1002; //id
item1002[1] = "Item name";
item1002[2] = 600; //item price
item1002[3] = "http://whatever"; //item page link

var item1003 = [];
item1003[0] = 1003; //id
item1003[1] = "Item name";
item1003[2] = 700; //item price
item1003[3] = "http://whatever"; //item page link

jQuery( "#items" ).change(function() {          
    var myItemPrice="item"+jQuery( "#items" ).val();
    console.log("Item price: "+myItemPrice);
    var total = window[myItemPrice][2];
    console.log("Total: "+total);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="items" id="items">
<option value="1001">Item name</option>
<option value="1002">Item name</option>
<option value="1003">Item name</option>
...
</select>

但您可以将项目存储在对象或数组中:

var items= {
  "1001": ["Item name", 500],
  "1002": [...],
  ...
}

您的事件处理程序会更容易:

jQuery( "#items" ).change(function() {          
    var myItem=jQuery( "#items" ).val();
    console.log("Item price: "+items[myItem][1]);
});

答案 3 :(得分:1)

<select name="items" id="items">
<option value="1001">Item name</option>
<option value="1002">Item name</option>
<option value="1003">Item name</option>
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
    var item = {
      1001 :{id:"1001", name:"Item name", price: 500, url:"http://whatever"},
      1002 :{id:"1002", name:"Item name", price: 600, url:"http://whatever"},
      1003 :{id:"1003", name:"Item name", price: 700, url:"http://whatever"}
    };

$( "#items").change(function(){
    var myItemPrice = item[$("#items").val()].price;
    console.log("Item price: "+myItemPrice);
    var total = myItemPrice - (myItemPrice*5)/100;
    console.log("Total: "+total);
});

</script>