Solved

Get Last Element in Array?

Posted on 2008-10-12
3
3,804 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now