Thursday, November 20, 2025
HomeLanguagesJavascriptCollect.js unique() Method

Collect.js unique() Method

The unique() method is used to return the all of the unique values in the collection.

Syntax:

collect(array).unique()

Parameters: The collect() method takes one argument that is converted into the collection and then unique() method is applied on it.

Return Value: This method returns all of the unique items in the collection.

Below example illustrate the unique() method in collect.js:

Example 1:




const collect = require('collect.js'); 
    
let arr = [1, 2, 3, 4, 5, 4, 3, 2, 1]; 
    
const collection = collect(arr); 
  
const unique = collection.unique();
    
let newObject =  unique.all();   
        
console.log(newObject);


Output:

[1, 2, 3, 4, 5]

Example 2:




const collect = require('collect.js'); 
    
let obj = [ 
    { 
        name: 'Kripamoy', 
        dob: '03-03-98', 
        section: 'A', 
        score: 94, 
    }, 
    { 
        name: 'Biltu', 
        dob: '23-01-96', 
        section: 'B', 
        score: 85, 
    }, 
    { 
        name: 'Santanu', 
        dob: '23-01-98', 
        section: 'B', 
        score: 98, 
    },
    { 
        name: 'chinmoy', 
        dob: '18-08-97', 
        section: 'A', 
        score: 72 
    } 
]; 
    
const collection = collect(obj); 
    
const unique = collection.unique('section');
    
let newObject =  unique.all();   
  
console.log(newObject);


Output:

[
 {name: "Kripamoy", dob: "03-03-98", score: 94, section: "A"},
 {name: "Biltu", dob: "23-01-96", score: 85, section: "B"}
]
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
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6904 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS