Lodash _.uniqBy method is similar to _.uniq except that it accepts iteratee which is invoked for each element in an array to generate the criterion by which uniqueness is computed. The order of result values is determined by the order they occur in the array.
Syntax:
_.uniqBy([array], [iteratee = _.identity])
Parameters:
- [arrays]: This parameter holds the arrays to inspect.
- [iteratee=_.identity]: This parameter holds the iteratee invoked per element.
Return Value:
This method is used to return the new duplicate free array.
Example:
javascript
const _ = require( "lodash" );
let y = ([2.4, 1.6, 2.2, 1.3]);
let gfg = _.uniqBy(y, Math.floor);
console.log(gfg);
|
Output:
[2.4, 1.6 ]
Example 2:
javascript
const _ = require( "lodash" );
let y = ([{ 'x' : 2 }, { 'x' : 2 }, { 'x' : 1 }]);
let gfg = _.uniqBy(y, 'x' );
console.log(gfg);
|
Output:
[ { 'x': 2 }, { 'x': 1 } ]
Example 3:
javascript
const _ = require( "lodash" );
let y = ([ 'aee' , 'bee' , 'bee' , 'cee' , 'eee' ,
'dee' , 'gee' , 'dee' ]);
let gfg = _.uniqBy(y);
console.log(gfg);
|
Output:
[ 'aee', 'bee', 'cee', 'eee', 'dee', 'gee' ]
Last Updated :
18 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...