jQuery 等价于 Prototype array.last()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1159978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery equivalent to Prototype array.last()
提问by guybrush
Prototype:
原型:
var array = [1,2,3,4];
var lastEl = array.last();
Anything similar to this in jQuery?
jQuery 中有类似的东西吗?
回答by Salty
Why not just use simple javascript?
为什么不只使用简单的 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];}
回答by guybrush
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/Arrayfor more information
有关更多信息,请参阅https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
回答by gnarf
When dealing with a jQuery object, .last()
will do just that, filter the matched elements to only the last one in the set.
在处理 jQuery 对象时,.last()
会这样做,将匹配的元素过滤到集合中的最后一个。
Of course, you can wrap a native array with jQuery leading to this:
当然,您可以使用 jQuery 包装本机数组,从而实现:
var a = [1,2,3,4];
var lastEl = $(a).last()[0];
回答by Muhan Alim
Why not use the get function?
为什么不使用 get 函数?
var a = [1,2,3,4];
var last = $(a).get(-1);
http://api.jquery.com/get/More info
http://api.jquery.com/get/更多信息
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.
编辑:正如 DelightedD0D 所指出的,这不是根据 jQuery 文档使用的正确函数,但它仍然提供正确的结果。我建议使用 Salty 的答案来保持您的代码正确。
回答by CMS
回答by martin
If u use the prototype on arrays like:
如果您在数组上使用原型,例如:
Array.prototype.last = function() {return this[this.length-1];}
using forloops will do this.
使用 forloops 可以做到这一点。
var a = [0,1,2];
out --> 0
out --> 1
out --> 2
out --> last
回答by Manticore
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.
对我来说很好用。
回答by Michael Blackburn
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.
根据jsPerf: Last item method,性能最好的方法是array[array.length-1]
. 该图显示的是每秒的操作次数,而不是每次操作的时间。
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.
开发人员认为单个操作的性能很重要是很常见的(但错误的)。它不是。性能仅在您进行大量相同操作时才重要。在这种情况下,使用静态值 ( length
) 访问特定索引 ( length-1
) 是最快的,甚至还差得远。
回答by fiedl
SugarJS
糖糖
It's not jQuery but another library you may find useful in addition to jQuery: Try SugarJS.
它不是 jQuery,而是除了 jQuery 之外您可能会发现有用的另一个库:尝试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.
Sugar 是一个 Javascript 库,它使用有用的方法扩展本机对象。它旨在直观、不引人注目,让您用更少的代码做更多的事情。
With SugarJS, you can do:
使用 SugarJS,您可以:
[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
更多信息
回答by Bharat Parmar
url : www.mydomain.com/user1/1234
网址:www.mydomain.com/user1/1234
$.params = window.location.href.split("/"); $.params[$.params.length-1];
$.params = window.location.href.split("/"); $.params[$.params.length-1];
You can split based on your query string separator
您可以根据查询字符串分隔符进行拆分