javascript - How to show array in reverse in VUE? - Stack Overflow

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

I have simple array with items in which I push new ones from time to time. New items es 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.

I have simple array with items in which I push new ones from time to time. New items es 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.

Share Improve this question edited Aug 25, 2017 at 12:21 Kin asked Aug 25, 2017 at 12:13 KinKin 4,59614 gold badges59 silver badges114 bronze badges 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. – enf0rcer Commented Aug 25, 2017 at 12:16
  • @user3492940 i dont need actually reverse it. I need only show in table it in reverse. – Kin 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. – enf0rcer 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 – Dean Coakley Commented Aug 25, 2017 at 12:19
  • @Deancoakley there are no such id. I need to use index instead – Kin Commented Aug 25, 2017 at 13:12
Add a ment  | 

2 Answers 2

Reset to default 5

Just use a puted 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: []
  },
  puted: {
    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./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>

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./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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745576939a4634022.html

相关推荐

  • javascript - How to show array in reverse in VUE? - Stack Overflow

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

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信