The contains() method is used to determines whether the collection contains a given item or not. If it contains the item then it returns true otherwise false. The JavaScript array is first transformed into a collection and then the function is applied to the collection.
Syntax:
collect(array).contains(item)
Parameters: This method accepts single parameter i.e. converted into the collection and then contains() function take an item to be searched.
Return Value: Returns a boolean value.
Below examples illustrate the contains() method in Collect.js:
Example 1: Here collect = require(‘collect.js’) is used to import the collect.js library into the file.
const collect = require( 'collect.js' );
let arr = [1, 2, 3]; // Convert array into collection const collection = collect(arr); // item to searched let item = 3; let newObject = collection.contains(item); console.log( "Result : " , newObject);
|
Output:
Result : true
Example 2:
const collect = require( 'collect.js' );
let arr = [1, 2, 3] // Convert array into collection const collection = collect(arr); // concat the array let concatarr = collection.concat([ 'a' , 'b' , 'c' ]);
// concat the object concatarr = concatarr.concat({ first : "GeeksforGeeks" ,
second : "Collect.js" });
// item to searched let item = "GeeksforGeek" ;
let newObject = collection.contains(item); console.log( "Result : " , newObject);
|
Output:
Result : false
Reference: https://collect.js.org/api/contains.html