![]() |
VOOZH | about |
Lodash _.head() method is used to get the first element of an array.
_.head( array );Example 1: In this example, we are getting the first element of the given array by the use of the _.head() method.
Output:
1Example 2: In this example, we are getting the first element of the given array of objects by the use of the _.head() method.
Output:
{ a: 1, b: 2 }Example 3: In this example, we are getting undefined as the given arrray is empty.
Output:
undefined Note: This will not work in normal JavaScript because it requires the library lodash to be installed.