Proposal: Array.prototype.first() and Array.prototype.last()

Nicu Micleusanu micnic90 at gmail.com
Tue Sep 27 12:12:34 UTC 2016


I propose to standardize `Array.prototype.first()` and 
`Array.prototype.last()`, very similar to underscore `_.first()` and 
`_.last()`.


A very basic implementation:

```js

Array.prototype.first = function (n) {

     if (!arguments.length) {

         return this[0];

     } else {

         return this.slice(0, Math.max(0, n));

     }

};


Array.prototype.last = function (n) {

     if (!arguments.length) {

         return this[this.length - 1];

     } else {

         return this.slice(Math.max(0, this.length - n));

     }

};

```



More information about the es-discuss mailing list