Wednesday, July 3, 2024
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!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments