Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptLodash _.union() Method

Lodash _.union() Method

Lodash _.union() method is used to take n number of arrays and create an array of unique values in order, from all given arrays using SameValueZero for equality comparisons.

Syntax: 

_.union(*arrays);

Parameters:

  • arrays: This parameter holds arrays to inspect.

Return Value:

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

Example 1: In this example, we are passing two different arrays into the _.union() method and printing the result in the console.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.union() method
let gfg = _.union([20, 12], [8, 15, 6]);
 
// Printing the output 
console.log(gfg);


Output:

[20, 12, 8, 15, 6]

Example 2: In this example, we are passing three different arrays into the _.union() method and printing the result in the console. 

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.union() method
let gfg = _.union([1, 2, 3],
    [3, 4, 5],
    [6, 2, 7]);
 
// Printing the output 
console.log(gfg);


Output:

[1, 2, 3, 4, 5, 6, 7]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments