For more tips like this, sign up to the weekly newsletter!

Use the index argument in Lodash FP

Iteratees for the array functions such as map, filter, and reduce get (element, index, array). But if you use Lodash FP, which provides a more functional way of writing and especially composing them, you'll soon find out that the signatures are different.

When you need only the element, these two are interchangeable:

[1, 2].map((e) => e + 1); // [2, 3]
_.map((e) => e + 1)([1, 2]); // [2, 3]

But when the other two arguments are also needed, Lodash FP falls short:

[1, 2].map((e, i) => i + 1); // [1, 2]
_.map((e, i) => i + 1)([1, 2]); // [NaN, NaN]

Lodash FP's map function is capped to only one argument.

Use convert to remove this capping:

_.map.convert({cap: false})((e, i) => i + 1)([1, 2]); // [1, 2]

The convert call returns a new function, so it does not have side effects. This also works for other arguments-capped functions.

Try it
References
Learn more: