title: How to append variables to an array in google scripts
body: <p>Right now I'm working on a project and I need to append a certain variable to an array. What I need to do is to read values from a spreadsheet and extract values that meet a certain criterion.</p>
  
  <p>In mathematica I will do it like this</p>
  
  <pre><code>If[IFin="RECIBIDO", 
  (*If true, do nothing*), 
  (*else*) AppendTo[pendientes,{"project#", "IFin", IFin}]
  ]
  </code></pre>
  
  <p>But I need to apply it in a google sheets script, so right now I'm trying this (I'm using a switch case structure because of my needs)</p>
  
  <pre><code>var IFin=0 //it can take the following values 0, 1, 2, 3, "RECIBIDO"
  var pendientes=[]; 
  var InfOK = "RECIBIDO";
  
  switch(IFin){
        case InfOk:
             break;
        case 0:
             [i,"IFin",IFin].appendTo(pendientes); 
             break;
        case 1:
             [i,"IFin",IFin].appendTo(pendientes); 
             break;
        case 2:
             [i,"IFin",IFin].appendTo(pendientes); 
             break;
        case 3:
             [i,"IFin",IFin].appendTo(pendientes); 
             break;
  }
  </code></pre>
  
  <p>And all it says when I try to run it is "TypeError: Could not find funcion appendTo". I'm new to google scripts, and hence I'm using this <a href="https://www.w3schools.com/jquery/html_appendto.asp" rel="nofollow noreferrer">https://www.w3schools.com/jquery/html_appendto.asp</a> I've searched the google help and it has an extensive explanation of sheets functions.</p>
  
  <hr>
  
  <p><strong>Solution</strong> </p>
  
  <p>This made me understand the root of the problem but proposed solution hasn't been tested yet:</p>
  
  <blockquote>
    <p>appendTo isn't an array method in javascript. You could do something
    like pendientes = pendientes.concat([i,"IFin",IFIN])</p>
  </blockquote>
  
  <p>This helped me solve the problem:</p>
  
  <blockquote>
    <p>Try using push(). This method adds one or more elements to the end of an array and returns the new length of the array.</p>
  </blockquote>
  
  <p>So by replacing </p>
  
  <pre><code>[i,"IFin",IFin].appendTo(pendientes);
  </code></pre>
  
  <p>by </p>
  
  <pre><code>pendientes.push([i,"IFin",IFin]);
  </code></pre>
  
  <p>my code worked just fine.</p>