Collect.js intersectByKeys() Method

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' }
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button