Last Updated : 30 Aug, 2024
Lodash _.concat() function is used to concatenate the arrays in JavaScript. and it returns the new concatenated array.
Syntax:_.concat(array, [values]);Parameters:
Return Value:Note: The array value can also contain arrays of an array or simply a single object to be added to the original array.
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4