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

后端 未结 6 1622
生来不讨喜
生来不讨喜 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:37

    For my use case (which is admittedly, apparently different than the OP...) I wanted to have the indices of the Array in reverse order in the v-for "loop."

    My solution was to create a Vue app method reverseRange(length) that returns an Array of integers from length-1 to 0. I then used that in my v-for directive and simply referred to my Array elements as myArray[index] every time I needed it.

    That way, the indices were in reverse order and I was able to then use them to access elements of the Array.

    I hope this helps someone who landed on this page with this subtle nuance in their requirements like me.

提交回复
热议问题