jQuery equivalent to Prototype array.last()
JqueryArraysJquery Problem Overview
Prototype:
var array = [1,2,3,4];
var lastEl = array.last();
Anything similar to this in jQuery?
Jquery Solutions
Solution 1 - 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];}
Solution 2 - Jquery
with 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
Solution 3 - Jquery
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];
Solution 4 - Jquery
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.
Solution 5 - Jquery
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.
Solution 6 - Jquery
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
Solution 7 - Jquery
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.
Solution 8 - Jquery
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.
Solution 9 - Jquery
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
Solution 10 - Jquery
url : www.mydomain.com/user1/1234
$.params = window.location.href.split("/"); $.params[$.params.length-1];
You can split based on your query string separator
Solution 11 - Jquery
You can use this Arr.slice(-1)[0]
.
Arr=[1,2,3,4,5,6,7]
Lets understand this. -1
means you are looking last index of Array.
so when you use Arr.slice(-1)[0]
then you will get result : 7
.
Solution 12 - Jquery
SugarJS
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
More Info
Solution 13 - Jquery
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.