Collect.js diffAssoc() Function

Last Updated : 29 Jul, 2020

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:

Comment