title: Dynamic Google Apps Script Array Construction failure
body: <p>Essentially I am looking through a previously created two dimensional array and wanting to output every value that matches a specific case in another two dimensional array.</p>
  
  <pre><code>function FruitsToEat() {
    var fruits = [
      ["Name",      "TastesGood?", "ShouldEat?"],
      ["Apple",     true,          true],
      ["Orange",    false,         true],
      ["Grapes',    true,          true],
      ["Forbidden", true,          false]
    ];
    var EatThese = [];
    var i = 0;
    var rows = 0;
    var columns = 0;
  
    for (i = 0; i &lt; fruits.length; i++) {        //Check the entire Array
      if (fruits[i][2] == true) {                //If the "ShouldEat?" column = true
        if (column != 2) {                       //Once column reaches 2, make a new row
          EatThese[row][column] = fruits[i][0];  //Set the cell to the fruit name
          column++;                              //Move to the right in the array
        } else {
          row++;                                 //Move down one in the array
          column = 0;                            //Move back to the left of the array
          EatThese[row][column] = fruits[i][0];  //Set the cell to the fruit name
          column++;                              //Move to the right in the array
        }
      }
    }
  
    return EatThese;
  }
  </code></pre>
  
  <p>The output is always:</p>
  
  <pre><code>TypeError: Cannot set property "0.0" of undefined to "Apple". (line 16, file "StackOverflow.com")
  </code></pre>
  
  <p>When I expect it to be</p>
  
  <pre><code>Apple Orange
  Grapes
  </code></pre>
  
  <p>In their respective cells in Sheets.</p>
  
  <p><strong>So I solved it myself while typing this up. I'll leave this here because I couldn't find help anywhere</strong></p>
  
  <p>I would still like to know why my initial idea did not work if somebody can tell me.</p>
  
  <pre><code>function FruitsToEat() {
    var fruits = [
      ["Name",      "TastesGood?", "ShouldEat?"],
      ["Apple",     true,          true],
      ["Orange",    false,         true],
      ["Grapes',    true,          true],
      ["Forbidden", true,          false]
    ];
    var EatThese = [];                          //This is our main array and will be what we return.
    var EatTheseTemp = [];                      //This is our temporary array. It will store each row of the main array until it is filled.
    var i = 0;
    var rows = 0;
    var columns = 0;
  
    for (i = 0; i &lt; fruits.length; i++) {        //Check the entire Array
      if (fruits[i][2] == true) {                //If the "ShouldEat?" column = true
        if (column != 2) {                       //Our fill detection
          EatTheseTemp[column] = fruits[i][0];   //Set the cell to the fruit name
          column++;                              //Move to the right in the temporary array
        } else {
          EatThese[row] = EatTheseTemp;          //Start constructing the main array
          row++;                                 //Prepare for the next fill
          column = 0;                            //Move back to the left of the temporary array
          EatTheseTemp = [];                     //Reset our temporary array
          EatTheseTemp[column] = fruits[i][0];   //Set the cell to the fruit name
          column++;                              //Move to the right in the temporary array
        }
      }
    }
  
    EatThese[row] = EatTheseTemp;
  
    return EatThese;
  }
  </code></pre>
  
  <p>Essentially I made two arrays. One array has pieces added to it each time the initial if statement returns true. Once the embedded if statement shows that this array has filled up, the complete row is added to the main array and then the other variables are wiped to start over. Outside of the for loop you need to add the final row and the two dimensional array is complete.</p>