Related Articles

Related Articles

Collect.js contains() Method
  • Last Updated : 29 Jul, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);

chevron_right


Output:

Result :  true

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

const collect = require('collect.js');  
      
let arr = [1, 2, 3]  
      
// Convert array into collection  
const collection = collect(arr);  
  
// concate the array
let concatarr = collection.concat(['a', 'b', 'c']);
  
// concate 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);

chevron_right


Output:

Result : false

Reference: https://collect.js.org/api/contains.html

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :