Open In App

Collect.js unique() Method

Last Updated : 14 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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"}
]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads