开发者

Why doesn't JavaScript have a last method? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the qu开发者_Python百科estion so it can be answered with facts and citations by editing this post.

Closed 9 years ago.

Improve this question

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.

Any serious reasons why this is not incorporated yet?


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.


It's easy to define one yourself. That's the power of 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..in loops to iterate over arrays.

However, if you are not bound with browser support problems, then using the new ES5 syntax to define properties can solve that issue, by making the function non-enumerable, like so:

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


Because Javascript changes very slowly. And that's because people upgrade browsers slowly.

Many Javascript libraries implement their own last() function. Use one!


i = [].concat(loves).pop(); //corn

icon cat loves popcorn


Another option, especially if you're already using UnderscoreJS, would be:

_.last([1, 2, 3, 4]); // Will return 4


Array.prototype.last = Array.prototype.last || function() {
    var l = this.length;
    return this[l-1];
}

x = [1,2];
alert( x.last() )


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.

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


Here is another simpler way to slice last elements

 var tags = [1, 2, 3, "foo", "bar", "foobar", "barfoo"];
 var lastObj = tags.slice(-1);

lastObj is now ["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.

Similarly, if you want the last two objects in a array,

var lastTwoObj = tags.slice(-2)

will give you ["foobar", "barfoo"] and so on.


pop() method will pop the last value out. But the problem is that you will lose the last value in the array


Yeah, or just:

var arr = [1, 2, 5];
arr.reverse()[0]

if you want the value, and not a new list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜