title: Reverse object with Object.keys and Array.reverse
body: <p>i got the following object</p>
  
  <pre><code>{
    "20170007": {
      "id": 1
    },
    "20170008": {
      "id" : 2
    },
    "20170009": {
     "id": 3
    },
    "20170010": {
      "id": 4
    }
  }
  </code></pre>
  
  <p>desired output:</p>
  
  <pre><code>{
    "20170010": {
      "id": 4
    },
    "20170009": {
     "id": 3
    },
    "20170008": {
      "id" : 2
    },
    "20170007": {
      "id": 1
    }
  }
  </code></pre>
  
  <p>my attempt:</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 obj = {
    "20170007": {
      "id": 1
    },
    "20170008": {
      "id" : 2
    },
    "20170009": {
     "id": 3
    },
    "20170010": {
      "id": 4
    }
  }
  
  const reverseObj = (obj) =&gt; {
    let newObj = {}
  
    Object.keys(obj)
      .sort()
      .reverse()
      .forEach((key) =&gt; {
        console.log(key)
        newObj[key] = obj[key]
      })
  
    console.log(newObj)
    return newObj  
  }
  
  reverseObj(obj)</code></pre>
  </div>
  </div>
  </p>
  
  <p>the wierd part is that when i console.log the key inside the forEach, the keys are reversed. but when i assign the key to the newObj the output is still in the original order...
  whats going on here ?</p>
  
  <p>EDIT:</p>
  
  <p>thanks for all the responses i took a look into The Map object.
  <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map" rel="noreferrer">Map</a></p>
  
  <pre><code>new Map([iterable])
  </code></pre>
  
  <p>Wich was actually i was looking for and the order is guaranteed.</p>