Sunday, November 17, 2024
Google search engine
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

Recent Comments