Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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?

like image 451
Nikhil Garg Avatar asked Jul 13 '10 07:07

Nikhil Garg


People also ask

Why doesn't JavaScript have a sleep function?

There's generally two reasons: 1) you're waiting for something else to finish, or 2) you want to control timing (a game loop or something). In the first case, if you block the UI thread then you won't even know if the other thing finished, since it will have to run on the UI thread, which is blocked.

Which built in method removes the last?

pop() method removes the last element from an array and returns that element.

How do you Unshift JavaScript?

unshift() The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

What is JavaScript last?

The last() method returns the last element of the selected elements. Tip: To return the first element, use the first() method.


9 Answers

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.

like image 122
Álvaro González Avatar answered Sep 29 '22 08:09

Álvaro González


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
like image 37
Anurag Avatar answered Sep 29 '22 08:09

Anurag


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

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

like image 39
Kenan Banks Avatar answered Sep 29 '22 08:09

Kenan Banks


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

icon cat loves popcorn

like image 29
Silviu-Marian Avatar answered Oct 02 '22 08:10

Silviu-Marian


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

_.last([1, 2, 3, 4]); // Will return 4
like image 44
Pablo Diaz Avatar answered Sep 30 '22 08:09

Pablo Diaz


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

x = [1,2];
alert( x.last() )
like image 32
meder omuraliev Avatar answered Oct 01 '22 08:10

meder omuraliev


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
like image 42
Daniel Avatar answered Sep 30 '22 08:09

Daniel


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.

like image 32
Prashant Avatar answered Sep 30 '22 08:09

Prashant


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

like image 41
Prashanth Shyamprasad Avatar answered Oct 03 '22 08:10

Prashanth Shyamprasad