Is there a notation to access the last element of an array in TypeScript? In Ruby I can say: array[-1]. Is there something similar?
11 Answers
You can access the array elements by its index. The index for the last element in the array will be the length of the array-1 (as indexes are zero based).
This should work:
var items: String[] = ["tom", "jeff", "sam"];
alert(items[items.length-1])
Here is a working sample.
4 Comments
As of July 2021, browsers are starting to support the at() method for Arrays which allows for the following syntax:
const arr: number[] = [1, 2, 3];
// shows 3
alert(arr.at(-1));
It's not clear to me at what point TypeScript will start to support this (it's not working for me just yet) but it should be available soon I would guess.
Edit: This is available as of [email protected]
3 Comments
If you don't need the array afterwards, you could use
array.pop()
But that removes the element from the array!
The pop returns T | undefined so you need to take care of that in your implementation.
If you are sure there will be always a value you can use non-null assertion operator (!):
var poped = array.pop()
array.push(poped!);
3 Comments
Type 'undefined' is not assignable to type 'string'const lastElem = myArray.map(...).filter(...).pop()Here is another way which has not yet been mentioned:
items.slice(-1)[0]
2 Comments
Here are a the options summarized together, for anyone finding this question late like me.
var myArray = [1,2,3,4,5,6];
// Fastest method, requires the array is in a variable
myArray[myArray.length - 1];
// Also very fast but it will remove the element from the array also, this may or may
// not matter in your case.
myArray.pop();
// Slowest but very readable and doesn't require a variable
myArray.slice(-1)[0]
1 Comment
slice really much slower than the others? pop is also a function call.If you need this call more often it's possible to declare it globally:
interface Array<T> {
last(): T | undefined;
}
if (!Array.prototype.last) {
Array.prototype.last = function () {
if (!this.length) {
return undefined;
}
return this[this.length - 1];
};
}
then you can just call
items.last()
2 Comments
One line Typescript function:
const last = <T>(arr: T[]): T | undefined => arr.at(-1);
if you use old ES versions:
function last<T>(arr: T[]): T | undefined {
return arr[arr.length - 1];
}
All fns have almost the the same perfomance. I recommend you to use arr[arr.length - 1] as the most native.
Comments
I'm going with this one as my first contribution to stackoverflow:
var items: String[] = ["tom", "jeff", "sam"];
const lastOne = [...items].pop();
NOTE: Unlike the use of pop() without the spread operator, this approach doesn't remove the last element from the original array.
3 Comments
there are several ways you could do this.
- By the index of the last element.
arr[arr.length - 1]
- Use the slice function: this function returns the value in an array that's why accessing the 0'th index is required.
arr.slice(-1)[0]
- Use the pop() function: this method is helpful when you need to get and remove the last element of the array. element will be returned and the last element will be removed from the original array.
arr.pop()
- Use the at() function. NOTE: This was introduced in ES22 and will not work in older versions
arr.at(-1)
You can pick any one of those methods based on your preferences.
Comments
const arr = [1, 3, 6, 2];
console.log(...arr.slice(-1)); // 2
