When grabbing from the end you can do array[array.length - x], where x is the number of items from the end you wanna grab.
For instance, to grab the last item you would use array[array.length - 1].
Videos
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/
Use the slice() method:
my_array.slice(-1)[0]
if (loc_array[loc_array.length - 1] === 'index.html') {
// do something
} else {
// something else
}
In the event that your server serves the same file for "index.html" and "inDEX.htML" you can also use: .toLowerCase().
Though, you might want to consider doing this server-side if possible: it will be cleaner and work for people without JS.
EDIT - ES-2022
Using ES-2022 Array.at(), the above may be written like this:
if (loc_array.at(-1) === 'index.html') {
// do something
} else {
// something else
}
Reference The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
arr.slice(-1)[0]
or
arr.slice(-1).pop()
Both will return undefined if the array is empty.