title: Why original array changes when I loop through the array with several for () function
body: <p>I have a simple javascript code. But it does not work as I expected.
  I want to extract data from javascript object but whenever I execute the function the original object array changes even though I did nothing with the original array.
  Is it the problem of the structure of the object array. Or async problem?</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;button onclick="test()"&gt; &lt;/button&gt;
  &lt;/body&gt;
  
  &lt;script type="text/javascript"&gt;
    var testArray = [];
  var num1 = 0;
      var allorderDemo = [
      {
          name:'test1',
          orders: [
          {name: 'fanta', num:1},
          {name: 'cola', num:2},
          {name: 'sweet', num:1}
          ]
      },
      {
          name:'test2',
          orders: [
          {name: 'fanta', num:1},
          {name: 'cola', num:2},
          {name: 'sweet', num:1}
          ]
      },
      {
          name:'test3',
          orders: [
          {name: 'fanta', num:1},
          {name: 'cola', num:2},
          {name: 'ox', num:1}
          ]
      }
      ];
       function test(){
          testArray = [];
          for(var i = 0; i &lt; allorderDemo.length; i++){
              for(var j = 0; j &lt; allorderDemo[i].orders.length; j++){
                  var index = testArray.findIndex(function(element) {
                          return (element.name == allorderDemo[i].orders[j].name);
                  });
                  console.log("index",index);
                  if(index &gt; 0) testArray[index].num += allorderDemo[i].orders[j].num;
                  else testArray.push(allorderDemo[i].orders[j]);
              }
          }
          console.log(allorderDemo.length);
        console.log(testArray);
        console.log(allorderDemo);
      }
  &lt;/script&gt;
  &lt;/html&gt;     
  </code></pre>
  
  <p>I want to extract testarray from allorderdemo. But whenever I execute test function 
  the console.log says the original array allorderdemo changes as i do not change it in the function.
  Why this error happens.
  Please help me.                    </p>