title: Javascript how to switch object keys or rename object key?
body: <p>I know how to change the value of a object but is it possible to change the name of a key or change the index position of a key?</p>
  <pre><code>const object1 = {
    &quot;a&quot;: 'helloa',
    &quot;b&quot;: 'hellob',
    &quot;c&quot;: 'helloc',
    &quot;d&quot;: 'hellod'
  };
  
  console.log(object1.b);
  // hellob
  
  What I want:
  // &quot;a&quot;: 'helloa'
  // &quot;c&quot;: 'helloc'
  // &quot;b&quot;: 'hellob'
  // &quot;d&quot;: 'hellod'
  </code></pre>
  <p>Solution:</p>
  <p>Help from @line-o</p>
  <p>The solution is to put all values ​​in an object and then change the order of the array. A sorting algorithm then changes the values ​​of the object with the values ​​of the array.</p>
  <pre><code>var object1 = { &quot;a&quot;: &quot;1&quot;, &quot;b&quot;: &quot;2&quot;, &quot;c&quot;: &quot;3&quot;, &quot;d&quot;: &quot;4&quot;, &quot;e&quot;: &quot;5&quot; }
  
  const order = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;x&quot;, &quot;d&quot;, &quot;e&quot;];
  
  // add new item in list
  object1[&quot;x&quot;] = &quot;6&quot; ;
  
  // Change order of items
  var a = order.indexOf(&quot;a&quot;);
  var e = order.indexOf(&quot;e&quot;);
  
  if (~a) {
      order[a] = &quot;e&quot;;
  }
  
  if (~e) {
      order[e] = &quot;a&quot;;
  }
  
  const sorted = Object
    .keys(object1)
    .sort(function (a, b) { 
      /* my sorting algo */
      const indexA = order.indexOf(a)
      const indexB = order.indexOf(b)
      
      return indexA - indexB;
  
    });
  
  object1 = order.map(key =&gt; object1[key]);
  
  
  console.log(object1);
  
  </code></pre>