获取json数组中的最后一个元素 [英] Get the last element in json array

查看:596
本文介绍了获取json数组中的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取seat对象中json数组中的最后一个元素。我想获得值为845的countryid,但是这个json是动态的,所以我想获得seat对象中的最后一个元素。我的api是这样构造的。先感谢您。

how can I get the last element in the json array in the seats object. I want to get the countryid of with the value of 845, however this json is dynamic so i want to get the last element in the seats object. My api is structured like this. Thank you in advance.

{
"expirationDate":"April 21, 2017",
"remainingDays":325,
"seats":[{"activeStatus":"S","pid":"TE70","firstName":"TE70","countryid":840},
        {"activeStatus":"Y","pid":"TE80","firstName":"TE80","countryid":845}]
 }


推荐答案

您可以通过索引访问 jsonData.seats 数组来实现此目的。最后一项等于 jsonData.seats.length-1

You can do this by accessing the jsonData.seats array by index, index of the last item being equal to jsonData.seats.length-1

简单地说:

var countryId = jsonData.seats[jsonData.seats.length-1].countryid

这篇关于获取json数组中的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆