Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.size Function

Underscore.js _.size Function

The Underscore.js is a JavaScript library that provides a lot of useful functions that help in programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.size() function is used to find the size of an array, i.e. the number of elements in the array. It is mostly used to find the number of elements in an array. 

Syntax:

_.size( list ) 

Parameters: This function accepts a single parameter list which is used to hold the list of items. 

Return values: This function returns a value that is the size of the passed array. 

Passing an array to the _.size() function: The ._size() function takes the element from the list one by one and calculate the size by starting from zero and incrementing by one as it passes through each element. Passing an array that has first the property name and then the element as one element of the array. If the property name will not be mentioned then the _.size() function will not work. 

Example: 

html




<!DOCTYPE html>
<html>
    <head>
        <script src =
        </script>
    </head>
    <body>
        <script type="text/javascript">
            var list = [1, 2, 3, 4, 5];
            console.log(_.size(list));
        </script>
    </body>
</html>


Output: 

Passing an array of large number of property to _.size() function: Passing a large array to the _.size() function. The _.size() function takes an array that has 4 properties, ‘category’, ‘title’, ‘value’, and ‘id’. Then apply _.size() function on this array and console.log() will display the final result. 

Example: 

html




<!DOCTYPE html>
<html>
    <head>
        <script src =
        </script>
    </head>
    <body>
        <script type="text/javascript">
            var goal = [
            {
                "category" : "other",
                "title" : "harry University",
                "value" : 50000,
                "id":"1"
            },
            {
                "category" : "travelling",
                "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(_.size(goal));
        </script>
    </body>
</html>


Output: 

Passing a structure to the _.size() function: First, declare an array (here array is ‘people’). Then just simply pass this array to the _.size() function and console.log() will display the final answer. 

Example: 

html




<!DOCTYPE html>
<html>
    <head>
        <script src =
        </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(_.size(people));
        </script>
    </body>
</html>


Output: 

Passing only one property array to the _.size() function: Declare an array that contains one property. The array ‘users’ is defined with one property ‘num’. Then simply pass this array to _.size() function by mentioning the array’s name inside the _.size() function. Finally, console.log() will display the final answer.

Example: 

html




<!DOCTYPE html>
<html>
    <head>
        <script src =
        </script>
    </head>
    <body>
        <script type="text/javascript">
            var users = [{"num":"1"},
                         {"num":"2"},
                         {"num":"3"},
                         {"num":"4"},
                         {"num":"5"}
        ];
        console.log(_.size(users));
        </script>
    </body>
</html>


Output:

RELATED ARTICLES

Most Popular

Recent Comments