title: How to update values in Javascript array based on another array?
body: <p>Not sure what's going on but can only guess its some weird reference issue rather than a copy. I have two arrays. One has an orderIndex I would like to update the sorted array values with.</p>
  <p>This is somehow making all the orderIndexes the first value (in this example 2). Anyone have any idea what is going on?</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const array = [{
    orderIndex: 2,
    name: "blah"
  }, {
    orderIndex: 1,
    name: "blah"
  }, {
    orderIndex: 3,
    name: "blah"
  }]
  
  
  function compare(a, b) {
    if (a.orderIndex &lt; b.orderIndex) {
      return -1;
    }
    if (a.orderIndex &gt; b.orderIndex) {
      return 1;
    }
    return 0;
  }
  
  // I make a sorted array
  tempArray = [...array]
  
  // this sorts the array based on orderIndex
  tempArray.sort(compare)
  
  // I then loop through and update the original array
  for (let i = 0; i &lt; array.length; i++) {
    array[i].orderIndex = tempArray[i].orderIndex
  }</code></pre>
  </div>
  </div>
  </p>