📜  Collect.js diffAssoc()函数

📅  最后修改于: 2022-05-13 01:56:25.446000             🧑  作者: Mango

Collect.js diffAssoc()函数

diffAssoc()函数用于比较两个给定集合并返回给定集合中不存在的值及其索引。如果集合中给出了键值对,它将根据指定的键进行比较并返回。

句法:

diffAssoc( collection );

参数:

  • 集合:它是给定的集合,其值将与原始数组进行比较。

返回值:它返回给定集合中不存在的值及其索引。

示例 1:当数组作为集合给出时

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())


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())


输出:

示例 2:当给定键值对的对象时。

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())

输出: