Open In App

Lodash _.concat() Function

Last Updated : 30 Aug, 2024
Suggest changes
Share
3 Likes
Like
Report

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
// Requiring the lodash library let lodash = require("lodash"); // Original array to be concatenated let array = [1, 2, 3]; // Values to be added to original array  let values = [0, 5, "a", "b"] let newArray = lodash.concat(array, values); console.log("Before concat: " + array); // Printing newArray  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
// Requiring the lodash library let lodash = require("lodash"); // Original array to be concatenated let array = ["a", 2, 3]; // Array of array to be added  // to original array let values = [0, 5, ["a", "b"]] let newArray = lodash.concat(array, values); console.log("Before concat: ", array); // Printing 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
// Requiring the lodash library let lodash = require("lodash"); // Original array to be concatenated let array = ["a", 2, 3]; // Object of values to be  // added to original array  let values = { "a": 1, "b": 2 } let newArray = lodash.concat(array, values); console.log("Before concat: ", array); // Printing array  console.log("After concat: ", newArray); 

Output:


Explore