Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting last element in JavaScript array [duplicate]

I'm making an application that updates a user's location and path in real time and displays this on a Google Map. I have functionality that allows multiple users to be tracked at the same time using an object, which is updated every second.

Right now, when a user pressed a button in the Android app, the coordinates are sent to a database and each time the location changes, a marker is updated on the map (and a polyline is formed).

Since I have multiple users, I send a unique and randomly generated alphanumeric string so that I can display an individual path for each user. When the JS pulls this data from the database, it checks if the user exists, if it does not, it creates a new key with the value being a list. It would look something like this:

loc = {f096012e-2497-485d-8adb-7ec0b9352c52: [new google.maps.LatLng(39, -86),                                               new google.maps.LatLng(38, -87),                                               new google.maps.LatLng(37, -88)],        44ed0662-1a9e-4c0e-9920-106258dcc3e7: [new google.maps.LatLng(40, -83),                                               new google.maps.LatLng(41, -82),                                               new google.maps.LatLng(42, -81)]} 

What I'm doing is storing a list of coordinates as the value of the key, which is the user's ID. My program keeps updating this list each time the location is changed by adding to the list (this works properly).

What I need to do is update the marker's location each time the location changes. I would like to do this by selecting the last item in the array since that would be the last known location. Right now, each time the location is changed a new marker is added to the map (each one of the points in the example would show a marker at that location) so markers continue to be added.

I would use a ´for (x in loc)` statement each time the location updates to grab the last location from the list and use that to update the marker. How do I select this last element from the array within the hash?

like image 768
mkyong Avatar asked Jan 29 '12 02:01

mkyong


People also ask

How do you select the last element of an array?

To get the last item without knowing beforehand how many items it contains, you can use the length property to determine it, and since the array count starts at 0, you can pick the last item by referencing the <array>. length - 1 item.

How will you change the last element of an array JavaScript?

Use the array. prototype. splice() to Remove the Last Element From an Array JavaScript. The splice() method is used to change the array by removing or replacing the elements.

How do you slice the last element in JavaScript?

To remove the last n elements from an array, use arr. splice(-n) (note the "p" in "splice"). The return value will be a new array containing the removed elements.


2 Answers

How to access last element of an array

It looks like that:

var my_array = /* some array here */; var last_element = my_array[my_array.length - 1]; 

Which in your case looks like this:

var array1 = loc['f096012e-2497-485d-8adb-7ec0b9352c52']; var last_element = array1[array1.length - 1]; 

or, in longer version, without creating new variables:

loc['f096012e-2497-485d-8adb-7ec0b9352c52'][loc['f096012e-2497-485d-8adb-7ec0b9352c52'].length - 1]; 

How to add a method for getting it simpler

If you are a fan for creating functions/shortcuts to fulfill such tasks, the following code:

if (!Array.prototype.last){     Array.prototype.last = function(){         return this[this.length - 1];     }; }; 

will allow you to get the last element of an array by invoking array's last() method, in your case eg.:

loc['f096012e-2497-485d-8adb-7ec0b9352c52'].last(); 

You can check that it works here: http://jsfiddle.net/D4NRN/

like image 92
Tadeck Avatar answered Oct 02 '22 12:10

Tadeck


Use the slice() method:

my_array.slice(-1)[0] 
like image 44
Datageek Avatar answered Oct 02 '22 13:10

Datageek