title: Array of subarrays of objects, how to not modify original array in function?
body: <p>In a small program I wrote I have an array with 3 subarrays each holding 4 objects. I put the array through a function that eliminates values from each subarray until there is only 1 object in each subarray left, and it returns an object containing the last 3 remaining objects.</p>
  
  <p>My problem is that the original array is modified in the process no matter what I do. I have tried the following:</p>
  
  <pre><code>    var itemsArr = [[obj1, obj2, obj3, obj4], [moreObj1, moreObj2, moreObj3, moreObj4],[lastObj1, lastObj2, lastObj3, lastObj4]];
  
      // each item in itemsArr subjarrays are objects containing name, url, and id keys.
  
          var generateResults = function(array, num){
          var arr = array.slice();    // generating copy of original array here (for some reason it isn't working 
  // have also tried arr = [].concat(array);
          var counter = 1;
          var holderObj;
          var results = {
            baby: null,
            husband: null,
            home: null
          };
          var tempArr;
          // iterate over outer array, containing arrays in each element
          var i = 0;
          while (true){
              if (arr[i].length === 1){
                  holderObj = arr.splice(i,1)[0][0];
                  i--; // accounting for reindexing of arr. could also iterate backwards if this doesn't work
                  results[holderObj.id] = holderObj;
              }
              if (arr[i]){
                  for (var j=0; j&lt;arr[i].length; j++){
                      if (counter === num){
                          arr[i].splice(j,1);
                          counter = 0;
                          j--;  // accounts for reindexing of arr[i]. Other solution would be iterating backwards
                      }
                      else{
                          counter ++;
                      }
                  }
              }
              if (results.baby !== null &amp;&amp; results.husband !== null &amp;&amp; results.home !== null){
                break;
              }
              else if (i === arr.length-1){
                  i = -1;
              }
  
              i++;
          }
          return results;
      }
  </code></pre>
  
  <p>how can i prevent my original itemsArr from being modified when i call generateResults(itemsArr, num);?</p>
  
  <p>I have also tried replacing the first line of generateResults with 
  var arr = [].concat(array);</p>