Wednesday, September 17, 2025
HomeLanguagesJavascriptCollect.js diffAssoc() Function

Collect.js diffAssoc() Function

The diffAssoc() function is used to compare two given collection and returns the values and their indices which are not present in the given collection. If key-value pair is given in the collection it compares and returns on the basis of keys specified.

Syntax:

diffAssoc( collection );

Parameters:

  • collection: It is the collection given whose values are to be compared from the original array.

Return Value: It returns the values and their indices which are not present in the given collection.

Example 1: When an array is given as a collection

Javascript




// Importing the collect.js module.
const collect = require('collect.js');
let array = ["a", "b", "c", "d", "e"];
let arr = ["a", "b", "q"];
  
// Making the collec1
let collec1 = collect(array);
let collec2 = collect(arr);
  
// Using diffAssoc() Function
let aa = collec1.diffAssoc(collec2)
  
// The output is so because c, d, e
// are not present in collec2 
// But are present in collec1
console.log("Output : ")
console.log(aa.all())


Output:

Example 2: When object of key-value pair is given.

Javascript




// Importing the collect.js module.
const collect = require('collect.js');
let obj1 = { "a": 1, "b": 12, "c": 3 };
let obj2 = { "a": 12, "d": 2, "c": 3 };
  
// Making the collec1
let collec1 = collect(obj1);
let collec2 = collect(obj2);
  
// Using diffAssoc() Function
let aa = collec1.diffAssoc(collec2)
  
// The output is so because a whose 
// value is 1 and b whose value is 12 
// because are not present in collec2 
// But are present in collec1
console.log("Output : ")
console.log(aa.all())


Output:

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
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11833 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7050 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS