Open In App

Lodash _.uniqBy() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let y = ([2.4, 1.6, 2.2, 1.3]);
 
// Use of _.uniqBy()
// method
 
let gfg = _.uniqBy(y, Math.floor);
 
// Printing the output
console.log(gfg);


Output: 

[2.4, 1.6 ]

Example 2: 

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let y = ([{ 'x': 2 }, { 'x': 2 }, { 'x': 1 }]);
 
// Use of _.uniqBy()
// method
// The `_.property` iteratee shorthand.
let gfg = _.uniqBy(y, 'x');
 
// Printing the output
console.log(gfg);


Output: 

[ { 'x': 2 }, { 'x': 1 } ] 

Example 3: 

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let y = (['aee', 'bee', 'bee', 'cee', 'eee',
 
    'dee', 'gee', 'dee']);
 
// Use of _.uniqBy()
// method
// The `_.property` iteratee shorthand.
let gfg = _.uniqBy(y);
 
// Printing the output
console.log(gfg);


Output: 

[ 'aee', 'bee', 'cee', 'eee', 'dee', 'gee' ] 


Last Updated : 18 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads