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