How do I reverse the order of an array using v-for and orderBy filter in Vue JS?

后端 未结 6 1615
生来不讨喜
生来不讨喜 2021-02-02 05:58

I am using Vue JS to do viewmodel bindings. In my data object I have an array of items that are sorted in ascending order (oldest to newest) and I\'d like to keep i

6条回答
  •  野的像风
    2021-02-02 06:36

    Note: The below works in Vue 1, but in Vue 2 filters are deprecated and you will see: ' Property or method "reverse" is not defined on the instance but referenced during render.' See tdom_93's answer for vue2.

    You could create a custom filter to return the items in reversed order:

    Vue.filter('reverse', function(value) {
      // slice to make a copy of array, then reverse the copy
      return value.slice().reverse();
    });
    

    Then use it in the v-for expression:

    https://jsfiddle.net/pespantelis/sgsdm6qc/

提交回复
热议问题