console.log('last', [1, 3, 4, 5].slice(-1));
console.log('second_to_last', [1, 3, 4, 5].slice(-2));
Answer from Ryan Huang on Stack Overflowconsole.log('last', [1, 3, 4, 5].slice(-1));
console.log('second_to_last', [1, 3, 4, 5].slice(-2));
I believe ES6 could at least help with that:
[...arr].pop()
Given your array (arr) is not undefined and an iterable element (yes, even strings work!!), it should return the last element..even for the empty array and it doesn't alter it either. It creates an intermediate array though..but that should not cost much.
Your example would then look like this:
console.log( [...['a', 'b', 'program']].pop() );
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.
Update 2021+
You can use the Array.at() method, which was moved to Stage 4 in Aug, 2021.
['a','b','c'].at(-1) // 'c'
This is often referred to as relative indexing which...
takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
Further Reading
Docs
- MDN - Array.prototype.at()
- Proposal - Relative Indexing Method
- tc39 spec - Array.at
- CanIUse - JavaScript built-in: Array: at
Stack Overflow
- Get the last item in an array
- Selecting last element in JavaScript array
- Destructuring to get the last element of an array in es6
just log the length minus 1, nothing to do with es6:
console.log(items[items.length - 1])