2015-09-02 395 views
-1

有人請讓我知道,我的嵌套循環的格式有什麼問題。我似乎沒有得到它正確地循環。相同的值不會一起生成。嵌套循環Javascript

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; 
      } 
     } 
    } 
+8

變量名'field'使用兩次。 –

回答

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

+0

非常感謝。夥計們。不知道我是如何錯過的 – BrodaTherapy