Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  123] [ 1]  / answers: 1 / hits: 71011  / 8 Years ago, tue, may 31, 2016, 12:00:00

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

More From » arrays

 Answers
6

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



simply:



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

[#61951] Sunday, May 29, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackelyn

Total Points: 303
Total Questions: 103
Total Answers: 102

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
jackelyn questions
Thu, Apr 8, 21, 00:00, 3 Years ago
Sun, Feb 28, 21, 00:00, 3 Years ago
Mon, May 25, 20, 00:00, 4 Years ago
Thu, Apr 30, 20, 00:00, 4 Years ago
;