Underscore.js _.contains Function

17 Dec, 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 _.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 _.contains() function.
Syntax: 
_.contains( list, value, [fromIndex] )
Parameters: This function accepts three parameters as mentioned above and described below:  
List: This parameter contains the list of items.
value: This parameter is used to store the value which need to search in the list.
fromIndex: It is an optional parameter that 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 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: 
HTML
<html>     <head>        <title>_.contains() function</title>        <script type="text/javascript" src =        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >        </script>        <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(_.contains([1, 2, 3, 4, 5], 40) );        </script>    </body></html>
Output: 
Example: 
HTML
<html>     <head>        <title>_.contains() function</title>        <script type="text/javascript" src =        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >        </script>        <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(_.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
<html>    <head>        <title>_.contains() function</title>        <script type="text/javascript" src=        "https://cdnjs.cloudflare.com/ajax/libs/        underscore.js/1.9.1/underscore-min.js" >        </script>        <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(_.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
<html>    <head>        <title>_.contains() function</title>        <script type="text/javascript" src=        "https://cdnjs.cloudflare.com/ajax/libs/         underscore.js/1.9.1/underscore-min.js" >        </script>        <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">             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 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:
HTML
<html>    <head>        <title>_.contains() function</title>        <script type="text/javascript" src=        "https://cdnjs.cloudflare.com/ajax/libs        /underscore.js/1.9.1/underscore-min.js" >        </script>        <script type="text/javascript" src=        "https://cdnjs.cloudflare.com/ajax/libs        /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: 
 
arorakashish0911
sweetyty
gulshankumarar231
JavaScript - Underscore.js
JavaScript
JQuery
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
How to append HTML code to a div using JavaScript ?
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to add options to a select element using jQuery?
jQuery | children() with Examples