1

I have simple array with items in which I push new ones from time to time. New items comes via ajax;

items.push({id: 1, title: 'a'});
items.push({id: 2, title: 'b'});
items.push({id: 3, title: 'c'});
items.push({id: 4, title: 'd'});

The main thing is that I need to show them in reverse like:

  1. 4, d
  2. 3, c
  3. 2, b
  4. 1, a

The problem is that I cant use unshift as I need to have index to items as I will need to do interactions with specific items.

So basically I'm searching for a way to push items but show them like it was unshift. Not actually reverse array.

5
  • You could use array.reverse, or array.reduceRight to reverse the array when you are done pushing values I guess. Using reduceRight you could even add the original index to the object so it is available as property. Commented Aug 25, 2017 at 12:16
  • @user3492940 i dont need actually reverse it. I need only show in table it in reverse. Commented Aug 25, 2017 at 12:17
  • so I can imagine you are using the v-repeat directive. Perhaps you could call array.reverse inline in the directive: <div v-repeat="item in array.reverse()"></div>. I don't use vue a lot but I can imagine this would work. Commented Aug 25, 2017 at 12:19
  • I am beginner at Vue but I think <li v-for="item in items | orderBy 'id' -1" track-by="id"> should work Commented Aug 25, 2017 at 12:19
  • @Deancoakley there are no such id. I need to use index instead Commented Aug 25, 2017 at 13:12

2 Answers 2

5

Just use a computed property and you're done. The reverse method will reverse the items in an array in place, so I used the slice method to create a new one.

As for the unshift thing, the update order is already there when you use the push method, so the order is just as simple as reverse the original array, you don't need to worry about it.

const app = new Vue({
  el: '#app',
  data: {
    items: []
  },
  computed: {
    reversedArr() {
      return this.items.slice().reverse()
    }
  },
  created() {
    this.items.push({id: 1, title: 'a'})
    this.items.push({id: 2, title: 'b'})
    this.items.push({id: 3, title: 'c'})
    this.items.push({id: 4, title: 'd'})
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.0/vue.js"></script>
<div id="app">
  <div>
    <h3>Original</h3>
    <ul>
      <li v-for="item in items">{{item}}</li>
    </ul>
  </div>
  <div>
    <h3>Reversed</h3>
    <ul>
      <li v-for="item in reversedArr">{{item}}</li>
    </ul>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

new Vue({
  el: '#example',
  data: {
    items:[]
  },
  mounted(){
    this.items.push({id: 1, title: 'a'})
    this.items.push({id: 2, title: 'b'})
    this.items.push({id: 3, title: 'c'})
    this.items.push({id: 4, title: 'd'})
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.0/vue.js"></script>

<div id="example">
  <div v-for="(item,index) in items.reverse()">
    {{index+1}}. {{item.id}},{{item.title}}
  </div>
</div>

5 Comments

[Vue warn]: You may have an infinite update loop in a component render function.
@Kin this code snippet works, so your error must be caused by a typo in your code. Are you sure you're calling arr.reverse() and not this.reverse()?
You can go even simpler by calling items.reverse() inline. <div v-for="(item, index) in items.reverse()">
beware reverse() modifies the array, so you first need to make a copy of it with slice() as mentioned in @kevguy's answer. Otherwise you will get unexpected results whenever you push new items.
⚠️ This is not a good solution. If you have arrays of any significant length you get the warning [Vue warn]: Maximum recursive updates exceeded.. Instead, use @kevguy s method instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.