Lodash proves to be much useful when working with arrays, strings, objects etc. It makes math operations and function paradigm much easier, concise. The _.concat() function is used to concatenating the arrays in JavaScript.
Syntax:
_.concat(array, [values])
Parameters: This function accept two parameters as mentioned above and described below:
- 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 array or simple a single object to be added to original array.
Return Value: This function returns the array after concatenation.
Few examples are given below for better understanding of the function.
Example 1:
Javascript
// Requiring the lodash librarylet lodash = require("lodash");// Original array to be concatenatedlet array = [1, 2, 3];// Values to be added to original arraylet values = [0, 5, "a", "b"]let newArray = lodash.concat(array, values);console.log("Before concat: " + array);// Printing newArrayconsole.log("After concat: " + newArray); |
Output:
Example 2: Adding array of array to original array.
JavaScript
// Requiring the lodash librarylet lodash = require("lodash");// Original array to be concatenatedlet array = ["a", 2, 3];// Array of array to be added// to original arraylet values = [0, 5, ["a", "b"]]let newArray = lodash.concat(array, values);console.log("Before concat: ", array);// Printing arrayconsole.log("After concat: ", newArray); |
Output:
Example 3: This example adding the object to the array.
Javascript
// Requiring the lodash librarylet lodash = require("lodash");// Original array to be concatenatedlet array = ["a", 2, 3];// Object of values to be// added to original arraylet values = { "a": 1, "b": 2 }let newArray = lodash.concat(array, values);console.log("Before concat: ", array);// Printing arrayconsole.log("After concat: ", newArray); |
Output:


