Open In App
Related Articles

Underscore.js _.contains Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Underscore.js _.contains() function is used to check whether a particular item is given in the list or 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 _.contains() function.

Syntax: 

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

Parameters:

  • List: This parameter contains the list of items.
  • value: This parameter is used to store the value that needs to be searched in the list.
  • fromIndex: It is an optional parameter that holds the index to start the search.

Return values:

This function returns the value that 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 elements from the list one by one and searches 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: In this example, we are passing an array to the Underscore.js _.contains function.

HTML

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

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: In this example, we are passing a list of strings to the Underscore.js _.contains function.

HTML

<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(['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: In this example, we are passing an array of array to the Underscore.js _.contains function.

HTML

<html>
 
<head>
    <title>_.contains() function</title>
    <script type="text/javascript" src=
        </script>
    <script type="text/javascript" src=
        </script>
</head>
 
<body>
    <script type="text/javascript">
        let 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 that contains other elements like numbers and also add this object as an array element. pass this array and the object to the _.contains() function. Since added the object is to the array hence the answer will be true.

Example: In this example, we are passing an object and array to the Underscore.js _.contains function.

HTML

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

                    

Output: 


 



Last Updated : 10 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads