Thursday, September 4, 2025
HomeLanguagesJavascriptCollect.js contains() Method

Collect.js contains() Method

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.

Javascript




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:

Javascript




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 : "neveropen",
        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

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6717 POSTS0 COMMENTS