admin管理员组文章数量:1026989
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:
- 4, d
- 3, c
- 2, b
- 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:
- 4, d
- 3, c
- 2, b
- 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.
- 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 useindex
instead – Kin Commented Aug 25, 2017 at 13:12
2 Answers
Reset to default 5Just 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>
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:
- 4, d
- 3, c
- 2, b
- 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:
- 4, d
- 3, c
- 2, b
- 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.
- 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 useindex
instead – Kin Commented Aug 25, 2017 at 13:12
2 Answers
Reset to default 5Just 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>
本文标签: javascriptHow to show array in reverse in VUEStack Overflow
版权声明:本文标题:javascript - How to show array in reverse in VUE? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745576938a2157077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论