Solved

Get Last Element in Array?

Posted on 2008-10-12
3
3,831 Views
Last Modified: 2008-10-12
Hi,

I have this ajax call that returns an array.  I need to somehow get the last value in the array.  How can I do this?  Here is the response from the ajax call.  The last element in the array contains the value "state_id_5".  I am trying to get this value out.   This value may change also so I can't hardcode it.  It may be state_id_7 or state_id_2.  It will always be the last element in the array but I just not sure how to extract that out....


Thanks,

-ws
r[154] =  new Array();
r[154][0] = 2116;
r[154][1] = "William Jessup University";
 
r[155] =  new Array();
r[155][0] = 2137;
r[155][1] = "Woodbury University";
 
r[156] =  new Array();
r[156][0] = 2141;
r[156][1] = "World Mission University";
 
r[157] =  new Array();
r[157][0] = "state_id_5";

Open in new window

0
Comment
Question by:Westside2004
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
gdarcan earned 500 total points
ID: 22698642
r[r.length-1][0]
0
 
LVL 7

Expert Comment

by:bluV11t
ID: 22698651
Try this:

var lastElementValue = r[r.length][0];
0
 
LVL 7

Expert Comment

by:bluV11t
ID: 22698667
Oups :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question