Prototype:
var array = [1,2,3,4];
var lastEl = array.last();
Anything similar to this in jQuery?
Why not just use simple javascript?
var array=[1,2,3,4];
var lastEl = array[array.length-1];
You can write it as a method too, if you like (assuming prototype has not been included on your page):
Array.prototype.last = function() {return this[this.length-1];}
array[-1]undefined. That's the same as Prototype's behavior: prototypejs.org/api/array/lastwith slice():
var a = [1,2,3,4];
var lastEl = a.slice(-1)[0]; // 4
// a is still [1,2,3,4]
with pop();
var a = [1,2,3,4];
var lastEl = a.pop(); // 4
// a is now [1,2,3]
see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array for more information
When dealing with a jQuery object, .last() will do just that, filter the matched elements to only the last one in the set.
Of course, you can wrap a native array with jQuery leading to this:
var a = [1,2,3,4];
var lastEl = $(a).last()[0];
Why not use the get function?
var a = [1,2,3,4];
var last = $(a).get(-1);
http://api.jquery.com/get/ More info
Edit: As pointed out by DelightedD0D, this isn't the correct function to use as per jQuery's docs but it does still provide the correct results. I recommend using Salty's answer to keep your code correct.
.get() - Retrieve the DOM elements matched by the jQuery object. . This is not meant to be used on an array like this.I know the answer is already given, but I think I've got another solution for this. You could take the array, reverse it and output the first array item like this:
var a = [1,2,3,4]; var lastItem = a.reverse()[0];
Works fine for me.
For arrays, you could simply retrieve the last element position with array.length - 1:
var a = [1,2,3,4];
var lastEl = a[a.length-1]; // 4
In jQuery you have the :last selector, but this won't help you on plain arrays.
If u use the prototype on arrays like:
Array.prototype.last = function() {return this[this.length-1];}
using forloops will do this.
var a = [0,1,2];
out --> 0
out --> 1
out --> 2
out --> last
According to jsPerf: Last item method, the most performant method is array[array.length-1]. The graph is displaying operations per second, not time per operation.
It is common (but wrong) for developers to think the performance of a single operation matters. It does not. Performance only matters when you're doing LOTS of the same operation. In that case, using a static value (length) to access a specific index (length-1) is fastest, and it's not even close.
See these test cases http://jsperf.com/last-item-method The most effective way is throug .pop method (in V8), but loses the last element of the array
I use this:
array.reverse()[0]
You reverse the array with reverse() and then pick the first item of the reversed version with [0], that is the last one of the original array.
You can use this code if you don't care that the array gets reversed of course, because it will remain so.
It's not jQuery but another library you may find useful in addition to jQuery: Try SugarJS.
Sugar is a Javascript library that extends native objects with helpful methods. It is designed to be intuitive, unobtrusive, and let you do more with less code.
With SugarJS, you can do:
[1,2,3,4].last() // => 4
That means, your example does work out of the box:
var array = [1,2,3,4];
var lastEl = array.last(); // => 4