Collect.js intersectByKeys() Method
Last Updated :
27 Nov, 2020
The intersectByKeys() method is used to remove any given keys from the original collection that are not present in the given array or collection.
Syntax:
collection.intersectByKeys(key)
Parameters: This method accepts single parameter as mentioned above and described below:
- key: This parameter holds the key or key, value pair that need to intersect from original collection.
Return Value: This method returns the intersected collection items.
Below example illustrate the intersectByKeys() method in collect.js:
Example 1:
const collect = require( 'collect.js' );
const collection = collect({
name: 'Rahul' ,
class: 'IX' ,
section: 'A' ,
score: 98
});
const intersect_val = collection.intersectByKeys({
name: 'Rakesh' ,
age: 24,
section: 'B' ,
year: 2011
});
console.log(intersect_val.all());
|
Output:
{ name: 'Rahul', section: 'A' }
Example 2:
const collect = require( 'collect.js' );
const collection1 = collect({
key11: 'val1' ,
key12: 'val2' ,
key13: 'val3'
});
const collection2 = collect({
key11: 'val1' ,
key22: 'val2' ,
key13: 'val3'
});
const intersect_val =
collection1.intersectByKeys(collection2);
console.log(intersect_val.all());
|
Output:
{ key11: 'val1', key13: 'val3' }
Share your thoughts in the comments
Please Login to comment...