javascript

Lodash - get the first element from an array

To get the very first item from an array using lodash, you can use its method head().

const arr = [10, 20, 30, 40, 50];
const first_item = _.head(arr);
// => 10

In the code snippet, we have defined an array arr and we want to get the very first element of this array.  We have used lodash method head() for this purpose which is returning 10 as the result.

_.head() Lodash

_.head(Array)

Array: [Required] The array whose first element you want to get.

Example Demo

Was this helpful?