[Vuejs]-Making object of items in decending order data object in vue.js

0๐Ÿ‘

โœ…

If you cannot / do not want to change your items dictionary into an array, you can simply use Object.keys() to extract its keys and work on them.

new Vue({
  el: '#app',
  data() {
    return {
      items: {
        // Note: even though you write them in descending order,
        // the JS engine will list them in ascending order.
        5: 'item_5',
        4: 'item_4',
        3: 'item_3',
      },
    };
  },
});
<script src="https://unpkg.com/vue@2"></script>

<div id="app">
  <p>itemsKeys: {{Object.keys(items)}}</p>
  <ol>
    <li v-for="(key, keyIndex) of Object.keys(items).reverse()">
      keyIndex: {{keyIndex}} / item key: {{key}} / item value: {{items[key]}}
    </li>
  </ol>
</div>

Leave a comment