title: How to pass an array to a function without mutating the original array?
body: <p>I'm working on a helper function within a bigger function.</p>
  <p>I have an array of team object (each team looks like this {team:<code>unique number of the team</code>, points:0, goalDiff:0, goalsFor:0}) as the first argument of my function and I have an array of game results as a second argument (each game looks like this [number ref home team, number ref away team, goals scored by home team, goals scored by away team]).</p>
  <p>My function looks at each game in the second array and allocate 2 points to the winning team and zero to the losing one. in case of draw, it allocates 1 point to each team. Function also counts the goals scored by each team.</p>
  <pre><code>    let computeGames = (arr, games) =&gt; {
  
    
      games.forEach((game) =&gt; {
        
        let homeTeamIndex = arr.findIndex(team =&gt; team.team === game[0])
        let awayTeamIndex = arr.findIndex(team =&gt; team.team === game[1])
        let homeTeam = arr[homeTeamIndex]
        let awayTeam = arr[awayTeamIndex]
      
        
        game[2] &gt; game[3]
          ? (homeTeam.points += 2)
          : game[2] === game[3]
          ? ((homeTeam.points += 1), (awayTeam.points += 1))
          : (awayTeam.points += 2);
        homeTeam.goalsFor += game[2];
        awayTeam.goalsFor += game[3];
        homeTeam.goalDiff += game[2] - game[3];
        awayTeam.goalDiff += game[3] - game[2];
      });
  
    return arr
      
    };
  </code></pre>
  <p>My code is counting the points and goals as expected, but my issue is that when I execute something like the code below, the points are being counted in my teams array as well as in my doNotMutate array.
  I do not understand why teams is being mutated given it's a copy of teams that is passed to the function, not the teams array itself. If someone could explain, I'm getting really confused here.</p>
  <p>Cheers</p>
  <pre><code>const teams = createTeams(number) // this is a helper function to generate my teams with all values = 0
  let doNotMutate = [...teams]
  
  doNotMutate= computeGames(doNotMutate,games)
    
    console.log(teams, doNotMutate)
  </code></pre>