Lodash _.uniq() method is used to create an array of unique values in order, from all given arrays using SameValueZero for equality comparisons.
Syntax:
_.uniq(array);
Parameter:
- array: This parameter holds an array to inspect.
Return Value:
This method is used to return the new array of combined values.
Example 1: In this example, we are getting an array having unique elements because of the lodash _.uniq() method.
javascript
const _ = require( "lodash" );
let y = ([1, 2, 2, 3, 4, 3, 8, 6]);
let gfg = _.uniq(y);
console.log(gfg);
|
Output:
[ 1, 2, 3, 4, 8, 6 ]
Example 2: In this example, we are getting an array having unique elements because of the lodash _.uniq() method.
javascript
const _ = require( "lodash" );
let y = ([ 'a' , 'b' , 'c' , 'e' , 'd' , 'd' , 'g' , 'i' , 'i' ]);
let gfg = _.uniq(y);
console.log(gfg);
|
Output:
[ 'a', 'b', 'c', 'e', 'd', 'g', 'i' ]
Example 3: In this example, we are getting an array having unique elements because of the lodash _.uniq() method.
javascript
const _ = require( "lodash" );
let y = ([ 'apple' , 'banana' , 'banana' , 'chikoo' ,
'Elderberry' , 'Damson' ,
'Date' , 'guava' , 'Damson Date' ]);
let gfg = _.uniq(y);
console.log(gfg);
|
Output:
[ 'apple', 'banana', 'chikoo', 'Elderberry', 'Damson', 'Date', 'guava', 'Damson Date' ]
Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.
Last Updated :
27 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...