Search code snippets, questions, articles...

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

Search Index Data (The code snippet can also be found with below search text)

_.head lodash
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet