Thursday, October 23, 2025
HomeLanguagesJavascriptLodash _.unionBy() Method

Lodash _.unionBy() Method

The _.unionBy() method accepts iteratee which is invoked for each element of each array to generate the criterion by which uniqueness is computed. The order of result values is determined from the first array in which the value occurs.

Syntax:

_.unionBy(array, [iteratee = _.identity])

Parameters: This method accepts two parameters as mentioned above and described below:

  • array: This parameter holds the array to inspect.
  • [iteratee = _.identity]: This parameter holds the iteratee invoked per element.

Return Value: This method is used to return the new array of combined values.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Use of _.unionBy() method 
let gfg = _.unionBy([{ 'y': 1 }], 
                    [{ 'y': 2 }, { 'y': 1 }], 
                    [{ 'y': 3 }], 'y'); 
        
// Printing the output  
console.log(gfg)


Output:

[ { 'y': 1 }, { 'y': 2 }, {'y': 3} ]

Example 2: 

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Use of _.unionBy() method 
let gfg = _.unionBy([1.5], 
                    [2.6, 2.7],
                    [2.3, 3.8], Math.floor); 
        
// Printing the output  
console.log(gfg)


Output:

[1.5, 2.6, 3.8]
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS