Lodash _.includes() Method
Last Updated :
25 Oct, 2023
Lodash _.includes() method is used to find whether the value is in the collection or not. If the collection is a string, it will be tested for a value sub-string, otherwise SameValueZero() method is used for equality comparisons. If the index is given and is negative, the value is tested from the end indexes of the collection as the offset.
Syntax:
_.includes( collection, value, index );
Parameters:
- collection: This parameter holds the collection to inspect. The collection can be an array object or string.
- value: This parameter holds the value to search for.
- index: This parameter holds the index to search from.
Return Value:
This method returns true if the value is found in the collection, else false.
Example 1: In this example, we are checking whether the given value is present in the given collection or not by the use of the _.includes() method.
Javascript
const _ = require( "lodash" );
let name = [ 'gfg' , 'geeks' ,
'computer' , 'science' , 'portal' ];
console.log(_.includes(name, 'computer' ));
console.log(_.includes(name, 'geeeks' ));
console.log(_.includes(name, 'gfg' , 2));
|
Output:
true
false
false
Example 2: In this example, we are checking whether the given value is present in the given collection or not by the use of the _.includes() method.
Javascript
const _ = require( "lodash" );
let name = [10, 15, 20, 25, 30];
console.log(_.includes(name, 25));
console.log(_.includes(name, 35));
console.log(_.includes(name, 25, 3));
|
Output:
true
false
true
Share your thoughts in the comments
Please Login to comment...