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
Logger.logwas used.