title: How to update an element of an array based on the index when using spread syntax?
body: <p>Consider an array as </p>
  
  <p><code>const foo = [1, 2, 3];</code></p>
  
  <p>Now if I want to replace the second element I can just do:</p>
  
  <p><code>foo[1] = 4;</code> or</p>
  
  <p><code>foo.splice(1,1,4);</code> or</p>
  
  <pre><code>const foo = [1, 2, 3];
  
  console.log([...foo.slice(0, 1), 4, ...foo.slice(2)]);
  // I know this creates a new array unlike the above two ways.
  </code></pre>
  
  <p>but when we use spread operator for shallow copying objects we can dynamically overwrite a property like:</p>
  
  <pre><code>const someObject = {
   a: 1,
   b: 2,
   c: 3
  }
  const propertyToChange = 'b';
  
  const newObject = { ...someObject, [propertyToChange]: 4 };
  </code></pre>
  
  <p>So, is there an equivalent of this for arrays? Maybe something like the following to change an element based on the index.</p>
  
  <pre><code>const newArray = [...oldArray, [dynamicIndex]: 4 ];
  </code></pre>