Skip to content
Related Articles

Related Articles

Improve Article
Underscore.js | contains() with Examples
  • Last Updated : 22 Oct, 2020

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 _.contains() function is used to check whether a particular item is given in the list of not. This function needs to pass the list to this function. If the list contains a large of items then simply define the list earlier and pass the name of the list as an argument to the _.contians() function.

Syntax:

_.contains( list, value, [fromIndex] )

Parameters: This function accepts three parameters as mentioned above and described below:

  • List: This parameters contains the list of items.
  • value: This parameter is used to store the value which need to search in the list.
  • fromIndex: It is optional parameter which holds the index to start search.

Return values: This function returns the value which is either true (when the element is present in the list) or false (when the element is not in the list).

  • Passing an array to the _.contains function(): The ._contains() function takes the element from the list one by one and search for the given element in the list. After the required element is found in the list while traversing the list, the contains() function ends and the answer is true otherwise answer is false.

    Example:






    <html>  
        <head>
            <title>_.contains() function</title>
            <script type="text/javascript" src
            </script>
            <script type="text/javascript" src=
            </script>
        </head>      
        <body>
            <script type="text/javascript">
                  console.log(_.contains([1, 2, 3, 4, 5], 40) );
            </script>
        </body
    </html>

    Output:

    Example:




    <html>  
        <head>
            <title>_.contains() function</title>
            <script type="text/javascript" src
            </script>
            <script type="text/javascript" src=
            </script>
        </head>      
        <body>
            <script type="text/javascript">
                  console.log(_.contains([1, 2, 3, 4, 5], 5) );
            </script>
        </body
    </html>

    Output:

  • Passing a list of strings to the _.contains() function: Pass the list of strings to the _.contains() function and check the given string is found in the list or not. If the string is present in the list then it returns true otherwise return false.

    Example:




    <html>
        <head>
            <title>_.contains() function</title>
            <script type="text/javascript" src=
            underscore.js/1.9.1/underscore-min.js" >
            </script>
            <script type="text/javascript" src=
             underscore.js/1.9.1/underscore.js">
            </script>
        </head>      
        <body>
            <script type="text/javascript">
                 console.log(_.contains(['steven',
                'mack', 'jack'], 'steven'));
            </script>
        </body>
    </html>

    Output:

  • Passing an array of array to _.contains() function: Create an array of array and pass the array name to the function to specify the element.

    Example:




    <html>
        <head>
            <title>_.contains() function</title>
            <script type="text/javascript" src=
             underscore.js/1.9.1/underscore-min.js" >
            </script>
            <script type="text/javascript" src=
             /underscore.js/1.9.1/underscore.js">
            </script>
        </head>       
        <body>
            <script type="text/javascript">
                 var indexes = [ 
                {'id': 1, 'name': 'simmy' }, 
                {'id':3, 'name': 'sam'},  
                {'id': 5, 'name': 'sandeep'}, 
                {'id': 1, 'name': 'sahil' }, 
                {'id':3, 'name': 'shagun'} 
            ];
            console.log(_.contains(indexes, {'id':1, 'name': 'jake'}));
            </script>
        </body>
    </html>

    Output:

  • Passing an object and an array to the _.contains() function: Firstly, define an object variable and assign it {test:”test”}. then create an array which contains other elements like numbers and also add this object as array element. pass this array and the object to the _.contains() function. Since added the object to the array hence the answer will be true.

    Example:




    <html>
        <head>
            <title>_.contains() function</title>
            <script type="text/javascript" src=
            /underscore.js/1.9.1/underscore-min.js" >
            </script>
            <script type="text/javascript" src=
            /underscore.js/1.9.1/underscore.js">
            </script>
        </head>       
        <html
        <body>
            <script type="text/javascript">
                 var testObject = {test:"test"};
            var testArray = [1, 2, 3, testObject];
            console.log(_.contains(testArray, testObject));
            </script>
        </body>
    </html>

    Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :