Underscore.js _.first() Function - GeeksforGeeks

24 Nov, 2021
The Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invokes, etc even without using any built-in objects.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.
Syntax:
_.first(array, [n]) 
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 have 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.
Example:
<html>    <head>        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js">    </script>    </head>    <body>        <script type="text/javascript">        console.log(_.first([{name: 'jack', age: 14},                              {name: 'jill', age: 15},                              {name: 'humpty', age: 16}]));        </script>    </body></html>                    
Output:
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 element 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.
Example:
<html>    <head>        <script type="text/javascript" src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >        </script>    </head>    <body>        <script type="text/javascript">            var goal  = [            {                "category" : "other",                "title" : "harry University",                "value" : 50000,                "id":"1"            },            {                "category" : "traveling",                "title" : "tommy University",                "value" : 50000,                "id":"2"            },            {                "category" : "education",                "title" : "jerry University",                "value" : 50000,                "id":"3"            },            {                    "category" : "business",                "title" : "Charlie University",                "value" : 50000,                "id":"4"            }        ]        console.log(_.first(goal));        </script>    </body></html>
Output:
Passing an array with one property as true/false to the _.first() function: This will work exactly the same as the above two examples. The false/true property will only be displayed in the first element. Here, this property will not be logically used.
Example:
<html>    <head>        <script type="text/javascript" src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >        </script>    </head>    <body>        <script type="text/javascript">            var people = [            {"name": "sakshi", "hasLong": "false"},            {"name": "aishwarya", "hasLong": "true"},            {"name": "akansha", "hasLong": "true"},            {"name": "preeti", "hasLong": "true"}        ]         console.log(_.first(people));        </script>    </body></html>
Output:
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 a result. Remember that the elements will always come from the start of the array.
Example:
<html>    <head>        <script type="text/javascript" src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >        </script>    </head>    <body>        <script type="text/javascript">             var users = [{"num":"1"}, {"num":"2"},              {"num":"3"}, {"num":"4"}, {"num":"5"}];        console.log(_.first(users, 2));        </script>    </body></html>
Output:
shubham_singh
arorakashish0911
JavaScript - Underscore.js
javascript-functions
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
How to create footer to stay at the bottom of a Web page?