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' }
