Underscore.js | contains() with Examples

The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke 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 list contains large of items then simply define the list earlier and pass the name of the list as 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>  
        <head>
            <title>contain() 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>

    chevron_right

    
    

    Output:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>  
        <head>
            <title>contain() 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>

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.