Collect.js | diff() Function

The diff() function compares the main collection against the given collection and returns the values which are in original collection but not 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.diff(collection)

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

  • collection: Hold the collection that will be compared with the main collection.

Return Value:  Returns a new collection with difference between collection items.
 

Below examples illustrate the diff() function in collect.js
Example 1: Here in this example, we take a collection and then using the diff() function and return the value not in the new collection.



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


 
Output:

Collection { items: [ 3, 4, 6 ] }

Example 2: There is one thing to notice that this function holds a collection, compare with the main collection but only return only those items that are extra in the main collection.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// It is used to import collect.js library
const collect = require('collect.js');
  
const col1 = [1, 2, 3, 4];
const col2 = [3, 4, 5, 6];
  
const x = collect(col1);
const y = collect(col2);
  
const difference = x.diff(y); 
console.log(difference.all());

chevron_right


Output:

[ 1 , 2]

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

full-stack-img

My Personal Notes arrow_drop_up

Check this out nerdboinet

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.