最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the last item of an array VueJS - Stack Overflow

programmeradmin6浏览0评论

I am working on a small VueJS webapp. I would like to output data from my array to the view but it has to be the last item of an array and of that last item the second item which is a and in my example equel to 39. I don't know how I can recieve that one.

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 },
        ]

    }
},

ponents: {
    DonutChart, BarChart, LineChart, AreaChart
},

created() {
    this.userLastCount = //equal to 39
}

I would like to output the last value of 'a' of the lineData object and assign it to a data string which I can output to the view. So, now the last 'a' = 39. But if I add another row in my object It has to be that one that is assigning to this.userLastCount

I am working on a small VueJS webapp. I would like to output data from my array to the view but it has to be the last item of an array and of that last item the second item which is a and in my example equel to 39. I don't know how I can recieve that one.

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 },
        ]

    }
},

ponents: {
    DonutChart, BarChart, LineChart, AreaChart
},

created() {
    this.userLastCount = //equal to 39
}

I would like to output the last value of 'a' of the lineData object and assign it to a data string which I can output to the view. So, now the last 'a' = 39. But if I add another row in my object It has to be that one that is assigning to this.userLastCount

Share Improve this question edited May 2, 2017 at 10:22 Giesburts asked May 2, 2017 at 10:13 GiesburtsGiesburts 7,64816 gold badges52 silver badges92 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

The last item of an array is arr[arr.length - 1]. You can use a puted to have that value always set for you, rather than maintaining a data item yourself:

puted: {
  userLastCount() {
    return this.lineData[this.lineData.length - 1].a;
  }
}

Ypu can do this uzing plain javascript

In your created() hook do as follows:

created(){
   //get the position of last object in the array.
    var lastPosition = this.lineData.length -1;

    this.userLastCount = this.lineData[lastPosition].a;
} 
发布评论

评论列表(0)

  1. 暂无评论