How do I reverse the order of an array using v-for

2019-03-23 08:41发布

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 it that way for code-based reasons.

var v = new Vue({
    el: '#app',
    data: {
        items: [
            {id: 51,  message: 'first'},
            {id: 265, message: 'second'},
            {id: 32,  message: 'third'}
        ],
    }
}

However, when I display the array in the template I'd like to reverse the order so that it's descending (newest to oldest). I tried the following:

<ol>
    <li v-for="item in items | orderBy -1" track-by="id">

This didn't work since the orderBy filter seems to require a field name as its first argument.

Is there any way to accomplish this in the template using the v-for syntax using the orderBy filter? Or am I going to have to create a custom reverse filter?

5条回答
放荡不羁爱自由
2楼-- · 2019-03-23 08:58

Simple and concise solution:

<li v-for="item in items.slice().reverse()">
//do something with item ...
</li>
查看更多
Viruses.
3楼-- · 2019-03-23 09:05

Update for Vue2

I want to show some ways that you can work with data and not using filters as they are deprecated in Vue2:

inside computed property

Use computed properties in place of filters, which is much better because you can use that data everywhere in component, not only just in template: jsFiddle

computed: {
    reverseItems() {
        return this.items.slice().reverse();
  }     
}

inside Vuex getter property

If you're using Vuex, and you store your data in store.state object. The best way do some transformation with data stored in state is to do that in getters object (for example filtering through a list of items and counting them, reverse order and so on...)

getters: {
    reverseItems: state => {
        return state.items.slice().reverse();
    }
}

and retrieve state from getters in component computed property:

computed: {
    showDialogCancelMatch() {
        return this.$store.state.reverseItems;
    }
}
查看更多
男人必须洒脱
4楼-- · 2019-03-23 09:11

The v-for directive doesn't support iterating backwards, so if you want to order by newest you're going to need to add another field to indicate when the item was added, or change id to increment every time an item is added.

Then, with field being the field indicting the order added:

<li v-for="item in items | orderBy 'field' -1" track-by="id">
查看更多
劫难
5楼-- · 2019-03-23 09:12

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.

查看更多
别忘想泡老子
6楼-- · 2019-03-23 09:18

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:

<ol>
    <li v-for="item in items | reverse" track-by="id">

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

查看更多
登录 后发表回答