嵌套循环Javascript

时间:2015-09-02 20:46:18

标签: javascript for-loop

有人可以告诉我,我的嵌套循环的格式有什么问题。我似乎没有让它正确循环。不会同时生成相同的值。

for (var field in Itemlist) {
        for (var field in EstItems){
            console.log(Itemlist[field].item_id, EstItems[field].zoho_id);
            if (EstItems[field].zoho_id == Itemlist[field].item_id) {
                console.log("We are In");
                var id = EstItems[field].itemID;
                var itemID = EstItems[field].zoho_id;
                var barcode = EstItems[field].barcode;
                //var EstBarcode = EstItems[field].itemID;
                var description = EstItems[field].description;
                var cost = EstItems[field].cost;
                var shippingCost = "500";
                var clearingCharges = "";
                var quantityOrdered = 1;
                //var quantityRecvd = EstItems[field].itemID;
                //var quantityRTD = EstItems[field].itemID;
                var selected = 0;
                var totalcost = (cost*quantityOrdered)+parseFloat(shippingCost);
                var categoryID = 0;
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

您通过覆盖上一个变量引入了范围问题。

for (var field in Itemlist) {
  // `field` here is a property from ItemList
  for (var field in EstItems){
    // `field` here is a property from EstItems
    // Any attempt to access the `field` var from the outer loop will fail, as it has been overwritten.
  }
}

为任一循环重命名field