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?
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <button onclick="test()"> </button>
</body>
<script type="text/javascript">
  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 < allorderDemo.length; i++){
            for(var j = 0; j < 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 > 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);
    }
</script>
</html>     
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.
 
     
     
    