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
32538 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6920 POSTS0 COMMENTS
Nicole Veronica
12033 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12142 POSTS0 COMMENTS
Shaida Kate Naidoo
7055 POSTS0 COMMENTS
Ted Musemwa
7289 POSTS0 COMMENTS
Thapelo Manthata
7010 POSTS0 COMMENTS
Umr Jansen
6997 POSTS0 COMMENTS