Lodash _.unionBy() Method
Lodash _.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:
- 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: In this example, we are combining multiple arrays of objects by using the _unionBy() method.
Javascript
const _ = require( "lodash" );
let gfg = _.unionBy([{ 'y' : 1 }],
[{ 'y' : 2 }, { 'y' : 1 }],
[{ 'y' : 3 }], 'y' );
console.log(gfg)
|
Output:
[ { 'y': 1 }, { 'y': 2 }, {'y': 3} ]
Example 2: In this example, we are combining multiple arrays by using the _unionBy() method.
Javascript
const _ = require( "lodash" );
let gfg = _.unionBy([1.5],
[2.6, 2.7],
[2.3, 3.8], Math.floor);
console.log(gfg)
|
Output:
[1.5, 2.6, 3.8]
Last Updated :
25 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...