Saturday, October 25, 2025
HomeLanguagesJavascriptCollect.js | intersect() Function

Collect.js | intersect() Function

The intersect() function is used to remove the value in the main collection which is not present in the given collection. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.

Syntax: 

data.intersect('x')

Parameters: This function accept a single parameter as mentioned above and described below:

  • x: Hold a collection that will intersect with the main collection .

Return Value:  Returns a modified or you can say intersected collection.
 

Below examples illustrate the intersect() function in collect.js
Example 1: Here in this example, we take a collection and then using the intersect() function take a new collection with which the value are intersected and a modified collection is returned.

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const collection = collect([1, 2, 3, 4, 5]);
const any = collection.intersect([1, 2, 3, 9]);
console.log(any.all());


Output:

[ 1, 2, 3 ]

Example 2:

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const collection = collect([9 , 8 , 7 , 6 , 5]);
new1 = collection.intersect([6 , 5 , 8 , 4]);
  
console.log(new1.all());


Output:

[ 8, 6, 5 ]

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS