Access last element of a TypeScript array

JavascriptTypescript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

You can access the array elements by it's 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.

Solution 2 - Javascript

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!);

Solution 3 - Javascript

Here is another way which has not yet been mentioned:

items.slice(-1)[0]

Solution 4 - Javascript

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]

Solution 5 - Javascript

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]

Solution 6 - Javascript

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()

Solution 7 - Javascript

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.

Solution 8 - Javascript

const arr = [1, 3, 6, 2];
console.log(...arr.slice(-1)); // 2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpitosalasView Question on Stackoverflow
Solution 1 - JavascriptShyjuView Answer on Stackoverflow
Solution 2 - JavascriptFabian ChantonView Answer on Stackoverflow
Solution 3 - Javascriptuser108828View Answer on Stackoverflow
Solution 4 - JavascriptSwarmpView Answer on Stackoverflow
Solution 5 - JavascriptJaymeView Answer on Stackoverflow
Solution 6 - JavascriptPetr SpacekView Answer on Stackoverflow
Solution 7 - JavascriptJuan Vicente Berzosa TejeroView Answer on Stackoverflow
Solution 8 - JavascriptMiroView Answer on Stackoverflow