为什么 JavaScript 没有 last 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3235043/
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
Why doesn't JavaScript have a last method?
提问by Nikhil Garg
Its kinda weird that the JavaScript Array class does not offer a last method to retrieve the last element of an array. I know the solution is simple (Ar[Ar.length-1] ), but, still, this is too frequently used.
JavaScript Array 类没有提供 last 方法来检索数组的最后一个元素,这有点奇怪。我知道解决方案很简单(Ar[Ar.length-1]),但是,这仍然太频繁使用。
Any serious reasons why this is not incorporated yet?
尚未合并的任何严重原因?
采纳答案by Triptych
Because Javascript changes very slowly. And that's because people upgrade browsers slowly.
因为 Javascript 变化非常缓慢。那是因为人们升级浏览器的速度很慢。
Many Javascript libraries implement their own last()function. Use one!
许多 Javascript 库实现了它们自己的last()功能。用一个!
回答by álvaro González
You can do something like this:
你可以这样做:
[10, 20, 30, 40].slice(-1)[0]
console.log([10, 20, 30, 40].slice(-1)[0])
The amount of helper methods that can be added to a language is infinite. I suppose they just haven't considered adding this one.
可以添加到语言中的辅助方法的数量是无限的。我想他们只是没有考虑添加这个。
回答by Anurag
It's easy to define one yourself. That's the power of JavaScript.
自己定义一个很容易。这就是 JavaScript 的力量。
if(!Array.prototype.last) {
Array.prototype.last = function() {
return this[this.length - 1];
}
}
var arr = [1, 2, 5];
arr.last(); // 5
However, this may cause problems with 3rd-party code which (incorrectly) uses for..inloops to iterate over arrays.
但是,这可能会导致(错误地)使用for..in循环来迭代数组的第 3 方代码出现问题。
However, if you are not bound with browser supportproblems, then using the new ES5 syntax to define propertiescan solve that issue, by making the function non-enumerable, like so:
但是,如果您不受浏览器支持问题的困扰,那么使用新的 ES5 语法来定义属性可以解决该问题,方法是使函数不可枚举,如下所示:
Object.defineProperty(Array.prototype, 'last', {
enumerable: false,
configurable: true,
get: function() {
return this[this.length - 1];
},
set: undefined
});
var arr = [1, 2, 5];
arr.last; // 5
回答by Silviu-Marian
i = [].concat(loves).pop(); //corn
i = [].concat(loves).pop(); //corn
icon cat loves popcorn
图标猫喜欢爆米花
回答by Pablo Diaz
Another option, especially if you're already using UnderscoreJS, would be:
另一种选择,特别是如果你已经在使用 UnderscoreJS,将是:
_.last([1, 2, 3, 4]); // Will return 4
回答by meder omuraliev
Array.prototype.last = Array.prototype.last || function() {
var l = this.length;
return this[l-1];
}
x = [1,2];
alert( x.last() )
回答by Daniel
Came here looking for an answer to this question myself. The slice answer is probably best, but I went ahead and created a "last" function just to practice extending prototypes, so I thought I would go ahead and share it. It has the added benefit over some other ones of letting you optionally count backwards through the array, and pull out, say, the second to last or third to last item. If you don't specify a count it just defaults to 1 and pulls out the last item.
来到这里是为了自己寻找这个问题的答案。切片答案可能是最好的,但我继续创建了一个“last”函数只是为了练习扩展原型,所以我想我会继续分享它。与其他一些相比,它具有额外的好处,让您可以选择通过数组向后计数,并拉出倒数第二个或倒数第三个项目。如果你没有指定计数,它只是默认为 1 并拉出最后一项。
Array.prototype.last = Array.prototype.last || function(count) {
count = count || 1;
var length = this.length;
if (count <= length) {
return this[length - count];
} else {
return null;
}
};
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr.last(); // returns 9
arr.last(4); // returns 6
arr.last(9); // returns 1
arr.last(10); // returns null
回答by Prashant
Here is another simpler way to slice last elements
这是切片最后一个元素的另一种更简单的方法
var tags = [1, 2, 3, "foo", "bar", "foobar", "barfoo"];
var lastObj = tags.slice(-1);
lastObjis now ["barfoo"].
lastObj现在是["barfoo"]。
Python does this the same way and when I tried using JS it worked out. I am guessing string manipulation in scripting languages work the same way.
Python 以相同的方式执行此操作,当我尝试使用 JS 时,它成功了。我猜测脚本语言中的字符串操作以相同的方式工作。
Similarly, if you want the last two objects in a array,
同样,如果你想要数组中的最后两个对象,
var lastTwoObj = tags.slice(-2)
will give you ["foobar", "barfoo"]and so on.
会给你["foobar", "barfoo"]等等。
回答by prashu132
pop()method will pop the last value out. But the problem is that you will lose the last value in the array
pop()方法将弹出最后一个值。但问题是你会丢失数组中的最后一个值
回答by TDahl
Yeah, or just:
是的,或者只是:
var arr = [1, 2, 5];
arr.reverse()[0]
if you want the value, and not a new list.
如果你想要这个值,而不是一个新列表。

