The _.first() function is used to return the first element of the array, i.e. the number at the zeroth index. It returns first n elements in the array of m size (n < m) by just passing the variable n in the array. It is a very easy to use function of the underscore.js library and is used widely when working with array elements.
Parameters: This function accepts two parameter as mentioned above and described below:
- array: This parameter is used to hold the array of elements.
- variable: It tells the number of elements wanted.
Return values: This function returns the array of elements.
Passing an array to the _.first function(): The ._first() function will return the first element along with all it’s properties of the array passed. Like here, the elements has 2 properties, the name and the age so, the final result will contain both of these properties of the first element as the variable n is not passed here.
Passing a structure to _.first() function: The ._first() function will return the first element along with all it’s properties of the array passed as the variable n is not passed here. Like here, each elements has four properties, the category, the title, the value and the id. So, the final result will contain all of these properties of the first element.
Passing an array with one property as true/false to the _.every() function: This will work exactly the same as the above two examples. The false/true property will only be displayed of the first element. Here, this property will not be logically used.
Passing an array with the variable n to the _.first() function: To have more than one element which is the first element, just pass the number and get result. Remember that the elements will always come from the start of the array.
- Difference between :first-child and :first-of-type selector in CSS
- How to merge the first index of an array with the first index of second array?
- jQuery | first() with Examples
- Flask - (Creating first simple application)
- Determine the first and last iteration in a foreach loop in PHP?
- How to use a not:first-child selector in CSS?
- CSS | :first-child Selector
- Getting started with React Native? Read this first !
- Introduction to HTML CSS | Learn to Design your First Website in Just 1 Week
- HTML Course | First Web Page | Printing Hello World
- CSS | :first-of-type Selector
- CSS | ::first-letter Selector
- CSS | ::first-line Selector
- PHP | Ds\Sequence first() Function
- PHP | Ds\Vector first() Function
- PHP | Ds\Deque first() Function
- PHP | Ds\Set first() Function
- Step by step guide to make your first Wordpress Plugin
- How to get first day of week in PHP?
- jQuery | :first-of-type Selector
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh