Lodash _.concat() Function
Last Updated :
20 Oct, 2023
Lodash _.concat() function is used to concatenate the arrays in JavaScript. and it returns the new concatenated array.
Syntax:
_.concat(array, [values]);
Parameters:
- array: It is an array to which values are to be added.
- values: It is the Array of values that is to be added to the original array.
Note: The array value can also contain arrays of an array or simply a single object to be added to the original array.
Return Value:
This function returns the array after concatenation.
Example 1: In this example, we are concatenating two arrays having numbers and string as a value by the use if the _concat() method.
Javascript
let lodash = require( "lodash" );
let array = [1, 2, 3];
let values = [0, 5, "a" , "b" ]
let newArray = lodash.concat(array, values);
console.log( "Before concat: " + array);
console.log( "After concat: " + newArray);
|
Output:
Example 2: In this example, we are concatenating two arrays having numbers,strings and sub array as a value by the use if the _concat() method.
JavaScript
let lodash = require( "lodash" );
let array = [ "a" , 2, 3];
let values = [0, 5, [ "a" , "b" ]]
let newArray = lodash.concat(array, values);
console.log( "Before concat: " , array);
console.log( "After concat: " , newArray);
|
Output:
Example 3: In this example, we are concatenating two arrays having numbers, strings, and object as a value by the use if the _concat() method.
Javascript
let lodash = require( "lodash" );
let array = [ "a" , 2, 3];
let values = { "a" : 1, "b" : 2 }
let newArray = lodash.concat(array, values);
console.log( "Before concat: " , array);
console.log( "After concat: " , newArray);
|
Output:
Share your thoughts in the comments
Please Login to comment...