We have some code that sits between an API and a google spreadsheet that exists to pull stock data in from the API, and display it in the spreadsheet. We get an API call, process it into a Javascript Object, and then do logic from there.
Unfortunately, for some specific items we're having issues with the stock level being zero, which is indicative of the for loops not working properly for us. Example below:
// Example data from API call
var data = {
"Item": [
{
"WarehouseQuantity": {
"WarehouseID": "New York",
"Quantity": "4"
},
"SKU": "PRODUCT-1"
}
]
}
// Loop through API data for each item
for (var x = 0; x < data.Item.length; x++) {
// Grab current item SKU
var currentSKU = data.Item[x].SKU;
var currentNewYorkStock = 0;
// Loop through list of warehouses and fetch QTY for each specific warehouse
for (var y = 0; y < data.Item[x].WarehouseQuantity.length; y++) {
// Warehouse - New York
if (data.Item[x].WarehouseQuantity[y].WarehouseID == "New York") {
Logger.log("Yes - New York has Stock");
currentNewYorkStock = data.Item[x].WarehouseQuantity[y].Quantity;
}
}
}
...
// End result is that we don't actually set the value for currentNewYorkStock,
and it ends up not reflecting the info fed from API (should be 4, is 0)
Essentially, the code for the second for loop is not running. This is working for other items, but not for some. Is there an issue with the way that the for loop is structured in regards to iterating through objects? I feel like I am missing something very simple.