Tuesday, September 24, 2024
Google search engine
HomeLanguagesJavascriptCollect.js duplicates() Method

Collect.js duplicates() Method

The duplicates() function in collect.js is used to extract the duplicates from the given array. It returns an object containing key as the index of the duplicate and value as the duplicate itself.

Installation:

  • Using NPM:
    npm install collect.js
  • Using CDN of collect.js
    <script src="https://cdnjs.com/libraries/collect.js"></script>
    

Syntax:

collect.duplicates()

Parameters: This method does not accept any parameters.

Return Value: It returns the object containing duplicate values and their indices.

Example 1:

Javascript




// Importing the collect.js module.
const collect = require('collect.js'); 
    
const collection = collect(['b', 'c', 'a', 'a', 'b', "c"]);
  
const duplicatesObject = collection.duplicates();
  
// Logging the return type
console.log("Type is: ", typeof(duplicatesObject))
  
// Logging the duplicates object
console.log(duplicatesObject)


Output:

Example 2: Logging using duplicate.all and extracting duplicates from an array of string.

Javascript




// Importing the collect.js module.
const collect = require('collect.js'); 
    
const collection = collect(["neveropen"
    "neveropen", "for", "neveropen", "for"]);
  
const duplicatesObject = collection.duplicates();
  
// Logging the return type
// logging the duplicates object using
// duplicate.all
console.log("Output: ")
console.log(duplicatesObject.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