Saturday, November 22, 2025
HomeLanguagesJavascriptCollect.js diffKeys() Function

Collect.js diffKeys() Function

Collect.js is a library in Javascript, which acts as a wrapper for arrays and objects. This library used to convert the array and objects into collections and then perform different operations on them.

The diffKeys method of collect.js converts the array into collections and then compares the collection against another collection based on its keys and returns elements in the original collection that are not present in another collection.

Installation:

  • In NodeJs:
    npm install collect.js
  • CDN for collect.js
    <script src="https://cdnjs.com/libraries/collect.js"></script>

Syntax:

collection.diffkeys(object);

Parameters: It takes only an object as a parameter.

Returns: It returns an object.

Below given are a few examples of this function

Example 1:

When another collection is empty object.




let collect=require("collect.js");
const someCollection = collect({
  "a":1,
  "b":2
});
// Applying diffkeys function
const diff = someCollection.diffKeys({
});
console.log("type of diff is: ", typeof(diff))
console.log("collection is: ", diff)


Output:

Example 2:

When another collection is not a empty object.




let collect=require("collect.js");
const someCollection = collect({
  "a":1,
  "b":2,
  "1":1,
  "2":2
});
// Applying diffkeys function
const diff = someCollection.diffKeys({
  "b":2,
  "2":2
});
console.log("type of diff is: ", typeof(diff))
console.log("collection is: ", diff)


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
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6849 POSTS0 COMMENTS