javascript - 获取数组 VueJS 的最后一项

标签 javascript arrays vue.js

我正在开发一个小型 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/

相关文章:

PHP - 从数据库构建多级关联数组(从数据库按州对城市进行排序)

javascript - vueJS 上的全局消息 (notif) 调用

javascript - 在 vuejs 或 javascript 中访问变量

javascript - 将 Unity WebGL 项目导入 Angular2 组件

javascript - 开 Jest 确认快照更改

javascript - 将数组转换为 Set 的时间复杂度 (Big-O)

arrays - 时间复杂度 : Findning the mode of an array

laravel - 如何在 Inertia.js 中操作浏览器 url?

javascript - Rails JS 模板不会呈现 javascript

javascript - Codewars Challenge - JavaScript - 查找数组中的第一个非连续数字