Lodash: _.first(array, n) and _.last(array, n)

Created on 9 Feb 2015  路  3Comments  路  Source: lodash/lodash

Underscore.js supports the usage of _.first and _.last in the following manner:

_.first(array, n)
_.last(array, n)

These give the first and last n items in an array respectively.
Lodash does not appear to support this syntax.

I am aware that Lodash offers take and takeRight, which give the same functionality. However for easy switch over from Underscore to Lodash, and to maintain Lodash as a superset of underscore, it would be best if these uses of _.first and _.last were supported.

Thanks!

invalid

Most helpful comment

These give the first and last n items in an array respectively.
Lodash does not appear to support this syntax.

Hi @sarahquigley!

Underscore uses _.take as an alias for _.first so you can use _.take(array, n) for cross-lib compat. For _.last although there isn't an Underscore alias you can use _.takeRight(array, n).

You can also use the 2.4.1 versions of the lodash.first or lodash.last packages.

However for easy switch over from Underscore to Lodash, and to maintain Lodash as a superset of underscore, it would be best if these uses of _.first and _.last were supported.

lodash has been around for almost 3 years now and has transitioned from following to leading. lodash now has more npm direct+indirect dependents than Underscore and in a little more than a month will be the most directly depended on package in npm. It is currently the most downloaded brand on npm (lodash + all its build flavors), accounting for around 3% of npm's daily download traffic, & it's gaining dependents at more than 2:1 of Underscore. In addition Underscore is now aligning more with lodash with additions such as _.keysIn, matching NaN values in _.indexOf, & customizable partial placeholders.

In this case the _.first and _.last methods were 3x overloaded by supporting no arguments, an n argument, and a predicate argument. This makes modularizing and optimizing the method more difficult because it has more API baggage to deal with. Splitting it out should clarify the functionality too. For example Underscore has _.take as an alias of _.first though _.take traditionally takes an n param only.

All 3 comments

@sarahquigley, first and last works the way you want in previous major version of LoDash. This is the one of features that differs in v2 and v3. You can look for others in Changelog.

Split _.first & _.last out into _.take, _.takeWhile, _.takeRight, & _.takeRightWhile for n & predicate use

These give the first and last n items in an array respectively.
Lodash does not appear to support this syntax.

Hi @sarahquigley!

Underscore uses _.take as an alias for _.first so you can use _.take(array, n) for cross-lib compat. For _.last although there isn't an Underscore alias you can use _.takeRight(array, n).

You can also use the 2.4.1 versions of the lodash.first or lodash.last packages.

However for easy switch over from Underscore to Lodash, and to maintain Lodash as a superset of underscore, it would be best if these uses of _.first and _.last were supported.

lodash has been around for almost 3 years now and has transitioned from following to leading. lodash now has more npm direct+indirect dependents than Underscore and in a little more than a month will be the most directly depended on package in npm. It is currently the most downloaded brand on npm (lodash + all its build flavors), accounting for around 3% of npm's daily download traffic, & it's gaining dependents at more than 2:1 of Underscore. In addition Underscore is now aligning more with lodash with additions such as _.keysIn, matching NaN values in _.indexOf, & customizable partial placeholders.

In this case the _.first and _.last methods were 3x overloaded by supporting no arguments, an n argument, and a predicate argument. This makes modularizing and optimizing the method more difficult because it has more API baggage to deal with. Splitting it out should clarify the functionality too. For example Underscore has _.take as an alias of _.first though _.take traditionally takes an n param only.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dwoldrich picture dwoldrich  路  3Comments

aj0strow picture aj0strow  路  3Comments

dengyaolong picture dengyaolong  路  3Comments

erykpiast picture erykpiast  路  3Comments

jcubic picture jcubic  路  3Comments