0

I'm trying to access the elements of an array inside the callback of a forEach block.

The idea is given a list of IDs from avail_id (this would finally come from an spreadsheet but for now I've taken some constant values to debug), I have to find all 'products' which can be produced.

Each product (given in prod_arr) requires two elements to be produced (given in the corresponding element of m1m2_arr)

The 'elements' required to produce any 'product' can be identified by numeric id.

avail_id is an array of all elements available.

For example prod_arr[1] ie "Carbon Polymers" require 'elements' identified by the id m1m2_arr[1] ie 16633 and 16636 to produce. Since both are available (as they are both elements of avail_id), "Carbon Polymers" can be produced. "Caesarium Cadmide" on the other hand cannot be produced.

Finally the function should return an array of all products that can be produced.

To achieve this, I'm first generating a list of ordered pairs of IDs that are available. Then checking that list against m1m2_arr.

In the forEach loop on avail_id_op I'm expecting retval to be populated with the result. However prod_arr[i] is returning empty. The if statement is being hit as seen by the match logs. But for some reason prod_arr is inaccessible.

function getProducible() {
  let avail_id = [16633, 16634, 16635, 16636]

  // Create a list of ordered pairs of ids
  let avail_id_op = []
  avail_id.forEach(function(id1) {
    avail_id.forEach(function(id2) {
      avail_id_op.push([id1,id2])
    })
  })

  // Technically, these arrays will be generated from the cells of the sheet. But the issue can be reproduced even if the arrays are defined directly

  let prod_arr = ["Caesarium Cadmide", "Carbon Polymers", "Ceramic Powder", "Crystallite Alloy", "Dysporite", "Fernite Alloy", "Ferrofluid", "Fluxed Condensates", "Hexite", "Hyperflurite", "Neo Mercurite", "Platinum Technite", "Promethium Mercurite", "Prometium", "Rolled Tungsten Alloy", "Silicon Diborite", "Solerium", "Sulfuric Acid", "Thulium Hafnite", "Titanium Chromide", "Vanadium Hafnite"]
  let m1m2_arr = [[16643.0, 16647.0], [16633.0, 16636.0], [16635.0, 16636.0], [16640.0, 16643.0], [16646.0, 16650.0], [16639.0, 16642.0], [16648.0, 16650.0], [16651.0, 16653.0], [16641.0, 16644.0], [16642.0, 16652.0], [16646.0, 16651.0], [16644.0, 16649.0], [16646.0, 16652.0], [16643.0, 16652.0], [16637.0, 16644.0], [16635.0, 16636.0], [16641.0, 16647.0], [16634.0, 16635.0], [16648.0, 16653.0], [16641.0, 16638.0], [16642.0, 16648.0]]
  Logger.log(m1m2_arr)
  Logger.log(avail_id_op)
  Logger.log(prod_arr)
  let retval = []

  avail_id_op.forEach(function(op) {
    Logger.log ("For op: "+op)
    for (let i=0; i<m1m2_arr.length; i++) {
      if ((m1m2_arr[i][0] == op[0]) && (m1m2_arr[i][1] == op[1])) {
        retval.push(prod_arr[i])
        Logger.log("Match at index: "+i+", prod: ", prod_arr[i])
      }
    }
  })

  return retval
}

Running the above code in the Google Apps Script Project editor gave me the following log:

9:11:46 PM  Info    [[16643.0, 16647.0], [16633.0, 16636.0], [16635.0, 16636.0], [16640.0, 16643.0], [16646.0, 16650.0], [16639.0, 16642.0], [16648.0, 16650.0], [16651.0, 16653.0], [16641.0, 16644.0], [16642.0, 16652.0], [16646.0, 16651.0], [16644.0, 16649.0], [16646.0, 16652.0], [16643.0, 16652.0], [16637.0, 16644.0], [16635.0, 16636.0], [16641.0, 16647.0], [16634.0, 16635.0], [16648.0, 16653.0], [16641.0, 16638.0], [16642.0, 16648.0]]
9:11:46 PM  Info    [[16633.0, 16633.0], [16633.0, 16634.0], [16633.0, 16635.0], [16633.0, 16636.0], [16634.0, 16633.0], [16634.0, 16634.0], [16634.0, 16635.0], [16634.0, 16636.0], [16635.0, 16633.0], [16635.0, 16634.0], [16635.0, 16635.0], [16635.0, 16636.0], [16636.0, 16633.0], [16636.0, 16634.0], [16636.0, 16635.0], [16636.0, 16636.0]]
9:11:46 PM  Info    [Caesarium Cadmide, Carbon Polymers, Ceramic Powder, Crystallite Alloy, Dysporite, Fernite Alloy, Ferrofluid, Fluxed Condensates, Hexite, Hyperflurite, Neo Mercurite, Platinum Technite, Promethium Mercurite, Prometium, Rolled Tungsten Alloy, Silicon Diborite, Solerium, Sulfuric Acid, Thulium Hafnite, Titanium Chromide, Vanadium Hafnite]
9:11:46 PM  Info    For op: 16633,16633
9:11:46 PM  Info    For op: 16633,16634
9:11:46 PM  Info    For op: 16633,16635
9:11:46 PM  Info    For op: 16633,16636
9:11:46 PM  Info    Match at index: 1, prod: 
9:11:46 PM  Info    For op: 16634,16633
9:11:46 PM  Info    For op: 16634,16634
9:11:46 PM  Info    For op: 16634,16635
9:11:46 PM  Info    Match at index: 17, prod: 
9:11:46 PM  Info    For op: 16634,16636
9:11:46 PM  Info    For op: 16635,16633
9:11:46 PM  Info    For op: 16635,16634
9:11:46 PM  Info    For op: 16635,16635
9:11:46 PM  Info    For op: 16635,16636
9:11:46 PM  Info    Match at index: 2, prod: 
9:11:46 PM  Info    Match at index: 15, prod: 
9:11:46 PM  Info    For op: 16636,16633
9:11:46 PM  Info    For op: 16636,16634
9:11:46 PM  Info    For op: 16636,16635
9:11:46 PM  Info    For op: 16636,16636
5
  • @imvain2 Please don't add Stack Snippet to Google Apps Script code, it should only used for executable HTML/CSS/JavaScript code Commented Jul 27, 2022 at 16:31
  • 1
    @Rubén, ok thanks for the info. I didn't think it was an issue since it is javascript, the only difference was the console. Commented Jul 27, 2022 at 16:32
  • console.log and Logger.log while they are similar, they don't always print the same output, considering this, since the OP is providing the logs, Logger.log should not be replaced by console.log. Commented Jul 27, 2022 at 16:34
  • As the logs of the Stack Snippet doesn't match the logs provided by the OP, I will revert the edit. Commented Jul 27, 2022 at 16:40
  • 1
    @TheMaster In this specific case the OP's issue is specific to how Logger.log was used. Commented Jul 27, 2022 at 16:48

1 Answer 1

3

The problem is the comma in Logger.log statements.

Logger.log("Match at index: "+i+" prod: ", prod_arr[i])

Replace the comma by +

Logger.log("Match at index: "+i+" prod: " + prod_arr[i])

Note: console.log and Logger.log are similar, but they don't work the exactly same way and don't always print exactly the same output (i.e. Date objects ).

Complete code showing the change

function getProducible() {
  let avail_id = [16633, 16634, 16635, 16636]

  // Create a list of ordered pairs of ids
  let avail_id_op = []
  avail_id.forEach(function(id1) {
    avail_id.forEach(function(id2) {
      avail_id_op.push([id1,id2])
    })
  })

  // Technically, these arrays will be generated from the cells of the sheet. But the issue can be reproduced even if the arrays are defined directly

  let prod_arr = ["Caesarium Cadmide", "Carbon Polymers", "Ceramic Powder", "Crystallite Alloy", "Dysporite", "Fernite Alloy", "Ferrofluid, Fluxed Condensates", "Hexite", "Hyperflurite", "Neo Mercurite", "Platinum Technite", "Promethium Mercurite", "Prometium", "Rolled Tungsten Alloy", "Silicon Diborite", "Solerium", "Sulfuric Acid", "Thulium Hafnite", "Titanium Chromide", "Vanadium Hafnite"]
  let m1m2_arr = [[16643.0, 16647.0], [16633.0, 16636.0], [16635.0, 16636.0], [16640.0, 16643.0], [16646.0, 16650.0], [16639.0, 16642.0], [16648.0, 16650.0], [16651.0, 16653.0], [16641.0, 16644.0], [16642.0, 16652.0], [16646.0, 16651.0], [16644.0, 16649.0], [16646.0, 16652.0], [16643.0, 16652.0], [16637.0, 16644.0], [16635.0, 16636.0], [16641.0, 16647.0], [16634.0, 16635.0], [16648.0, 16653.0], [16641.0, 16638.0], [16642.0, 16648.0]]
  Logger.log(m1m2_arr)
  Logger.log(avail_id_op)
  Logger.log(prod_arr)
  let retval = []

  avail_id_op.forEach(function(op) {
    Logger.log ("For op: "+op)
    for (let i=0; i<m1m2_arr.length; i++) {
      if ((m1m2_arr[i][0] == op[0]) && (m1m2_arr[i][1] == op[1])) {
        retval.push(prod_arr[i])
        Logger.log("Match at index: "+i+" prod: " + prod_arr[i])
      }
    }
  })

  return retval
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.