我正在开发一个小型 VueJS 网络应用程序。我想将数组中的数据输出到 View ,但它必须是数组的最后一项,最后一项是第二项,在我的示例中等于 39。我不知道我怎么能收到那个。
HTML
<p>The last number in the array (a) is {{userLastCount}} </p>
Javascript/Vue
data () {
return {
event: 'Custom event',
userLastCount: 0,
lineData: [
{ time: '2017-05-01 15:00', a: 0 },
{ time: '2017-05-01 16:00', a: 12 },
{ time: '2017-05-01 17:00', a: 23 },
{ time: '2017-05-01 18:00', a: 28 },
{ time: '2017-05-01 19:00', a: 39 },
]
}
},
components: {
DonutChart, BarChart, LineChart, AreaChart
},
created() {
this.userLastCount = //equal to 39
}
我想输出 lineData 对象的“a”的最后一个值,并将其分配给一个数据字符串,我可以将其输出到 View 。所以,现在最后一个 'a' = 39。但是如果我在我的对象中添加另一行它必须是分配给 this.userLastCount
最佳答案
数组的最后一项是arr[arr.length - 1]
。您可以使用 computed
始终为您设置该值,而不是自己维护数据项:
computed: {
userLastCount() {
return this.lineData[this.lineData.length - 1].a;
}
}
关于javascript - 获取数组 VueJS 的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735309/