0

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.

Nimna Perera
  • 1,015
  • 10
  • 16
Akjm
  • 53
  • 5
  • 1
    `warehouseQuantity` is an object not an array. so you can't use `length` property on it – DecPK Apr 19 '21 at 05:20
  • 1
    You’re treating the “WarehouseQuantity” property like an array, but it’s just a plain old object. See data.Item[x].WarehouseQuantity[y] <— this will not work – user3163495 Apr 19 '21 at 05:20
  • To find such an issue yourself next time I'd recommend running your code in a debugger such as [Chrome's built-in one](https://developer.chrome.com/docs/devtools/javascript/) and stepping through the code line by line, looking at the values of variables at each step, to understand why the code does what it does. Where it differs from your expectations, you found the bug. In this case you'd find that the loop never enters its body, and when you verify the loop condition you'd see that the `length` is `undefined` and `0 < undefined` is `false`, and then you'd see `WarehouseQuantity` is an object – CherryDT Apr 19 '21 at 05:24
  • Does this answer your question? [Length of a JavaScript object](https://stackoverflow.com/questions/5223/length-of-a-javascript-object) – chiliNUT Apr 19 '21 at 06:10

1 Answers1

0

If the WarehouseQuantity property is a list containing inventory of some cities, you may need to modify the data structure like this:

// Example data from API call
var data = {
  "Item": [
    {
      "WarehouseQuantity": [{
        "WarehouseID": "New York",
        "Quantity": "4"
      },
      {
        "WarehouseID": "DC",
        "Quantity": "2"
      }],
      "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") {
            console.log("Yes - New York has Stock");
            currentNewYorkStock = data.Item[x].WarehouseQuantity[y].Quantity;
        }
    }
}
coderLMN
  • 3,076
  • 1
  • 21
  • 26