title: How to loop, concatenate and push content into an array in JavaScript
body: <p>I'm trying to dynamically generate an array and concatenate the results without turning the google maps function into strings. </p>
  
  <p>Any suggestions on the best way to do this would be great.</p>
  
  <p>Here is a snippet of my code:</p>
  
  <pre><code>function goCheck() { // find out what checkboxes in the form were selected
  
  //set a few variables and arrays
  var input = document.getElementsByTagName('input');
  var checkboxCount = 0;
  var selections = [];
  var urlArray = [];
  
  // 1st loop to get form checkbox selections
  for (var i=0, length = input.length; i&lt;length; i++) { 
       if (input[i].checked === true) {
       s = input[i].value;
       selections.push(s); // store values for checkbox selections in array
       checkboxCount++; // keep track how many were checked
  
       var url = "https://mywebsite.ca/" + s + ".txt";
       urlArray.push(url); // store urls in array
     }
  }
  
  console.log(checkboxCount); // number of boxes checked
  console.log(selections); // array with the selections
  console.log(urlArray); // an array with the json urls
  
  // 2nd Loop  - iterate over URL array and call function to get json object
  
  var xmlhttp;
  var myArr;
  for (var i=0, length = urlArray.length; i&lt;length; i++) {
      xmlhttp = new XMLHttpRequest(); //
      console.log(xmlhttp);
      xmlhttp.onreadystatechange = function() { 
           if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {
                var myArr = JSON.parse(xmlhttp.response);
  
                console.log(myArr);
                console.log(myArr[selections[i]]);
                console.log(myArr[selections[i]].length); 
  
  
  /* this is where I'm getting into trouble -----&gt;
  
  
                var arraySize = (myArr[selections[i]].length); 
                for(k=0; k&lt;arraySize; k++) {  
                district = "new google.maps.LatLng("+myArr[selections[i]][0]+")"; 
                polyLayer.push(district);
  
  
   &lt;----- */  
  
           }
         } 
      };
  
      xmlhttp.open("GET", urlArray[i], false);
      xmlhttp.send();  
  }}
  </code></pre>