The Wayback Machine - https://web.archive.org/web/20241004013435/https://www.geeksforgeeks.org/lodash-_-concat-function/
Open In App

Lodash _.concat() Function

Last Updated : 30 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

Lodash _.concat() Function – FAQs

What does the _.concat() function do in Lodash?

The _.concat() function creates a new array by concatenating the given array with additional arrays or values.

How do you use the _.concat() function in Lodash?

Call _.concat(array, [values]) where array is the initial array and values are additional arrays or values to concatenate.

Does _.concat() modify the original array?

No, _.concat does not modify the original array; it returns a new array with the concatenated values.

Can _.concat() combine multiple arrays at once?

Yes, _.concat can combine multiple arrays and values at once, e.g., _.concat([1], [2, 3], [4]);.

What are common use cases for _.concat()?

Common uses include merging arrays, adding elements to arrays, and constructing new data sets.



Previous Article
Next Article

Similar Reads

Tensorflow.js tf.concat() Function
Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. The tf.concat() function is used to concatenate the list of specified Tensors along the given axis. Syntax: tf.concat (tensors, axis)Parameters: This function accepts two parameters wh
2 min read
p5.js concat() function
The concat() function in p5.js is used to concatenate the two given arrays. This function is depreciated from future versions and uses First_array.concat(Second_array) instead. Syntax: concat(First_array, Second_array) Parameters: This function accepts two parameters as mentioned above and described below: First_array: The first array will be conca
1 min read
Node.js Buffer.concat() Method
The Buffer.concat() method is used to concat all buffer objects in a given array into one buffer object. The return value of this method is also a buffer. If the length of the buffer is not provided then it is calculated from the Buffer instances in the list. Syntax: Buffer.concat( list, length ) Parameters: This method accepts two parameters as me
2 min read
Collect.js concat() Method
The concat() method is used to return the merged arrays or objects represented by the collection. The JavaScript array is first transformed into a collection and then the function is applied to the collection. Syntax: collect(array1).concat(array2) Parameters: The collect() method takes one argument that is converted into the collection and then co
2 min read
Ember.js Ember.Templates.helpers concat() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
2 min read
Difference Between Spread Operator and Array.concat() in Typescript
The spread operator and concat() method both are used to add elements to an array. But they are different from each other in many ways. Let us discuss the difference between both of them in detail. Spread OperatorThe spread operator creates a new array by merging the elements of the passed arrays or the values. It is denoted by three dots(...). It
3 min read
TypeScript String concat() Method
The concat() method in TypeScript merges two or more strings into one new string. It takes multiple string arguments and concatenates them in the order provided, returning the combined result. Syntaxstring.concat(string2, string3[, ..., stringN]); Parameter: This method accepts a single parameter as mentioned above and described below. string2...st
2 min read
JavaScript Array concat() Method
The concat() method concatenates (joins) two or more arrays. It returns a new array, containing the joined arrays. This method is useful for combining arrays without modifying the originals. Syntax:let newArray1 = oldArray.concat()let newArray2 = oldArray.concat(value0)let newArray3 = oldArray.concat(value0,value1)..............let newArray = oldAr
3 min read
TypeScript Array concat() Method
The TypeScript Array.concat() method merges two or more arrays into a new array. It takes multiple arrays and/or values as parameters and returns a single array containing all the elements. This method helps efficiently combine data from various sources into one array. Syntaxarray.concat(value1, value2, ..., valueN)Parameter: This method accepts a
2 min read
How to concat strings using AngularJS ?
In this article, we will see how to concat strings in AngularJS. There are few ways to concat the strings in AngularJS. In this article, we will see 2 of them. Example 1: In the first example, we are using the '+' operator to concat the strings [GFGTABS] HTML <!DOCTYPE HTML> <html> <head> <script src= "https://ajax.googlea
2 min read
JavaScript String concat() Method
The concat() method in JavaScript join or concatenate two or more strings together. It does not change the existing strings and returns a new string. This method efficiently combines multiple strings into one, ensuring the original strings remain unchanged. Syntaxstr1.concat(str2, str3, str4,......, strN)Parametersstr1, str2, ..., strN: This method
3 min read
Node.js lodash.sortBy() Function
Lodash is a module in Node.js that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The Loadsh.sortBy() function is used to sort the array in ascending order. Syntax: sortBy(collection, [iteratees=[_.identity]]) Parameters: This parameter holds the collection as a first parameter, Second parame
1 min read
Lodash _.isSequential() Function
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.isSequential() method is used to check whether the given value is a sequential composite type value or not. The examples of a sequential composite type are arrays and arguments. Syntax: _.isSequential( va
2 min read
How to Import a Single Lodash Function in JavaScript ?
In JavaScript, we have to import various functions for operation, but importing a single function is also important for optimizing code and reducing unnecessary dependencies. Below are the approaches to importing a single Lodash function in JavaScript: Table of Content Using ES6 import syntaxUsing CommonJS require syntaxRun the below command before
2 min read
How to Import a Single Lodash Function?
Lodash is a popular JavaScript utility library that provides a variety of useful functions for common programming tasks such as manipulating arrays, objects, and strings. However, importing the entire Lodash library can significantly increase the size of your JavaScript bundle, which can affect performance. Instead, you can import only the Lodash f
2 min read
Lodash Function Complete Reference
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. It provides us with various inbuilt functions and uses a functional programming approach which makes coding in javascript easier to understand because instead of writing repetitive functions, tasks can be accom
2 min read
Why is lodash.each() function Faster than Native forEach?
Sometimes, When working with JavaScript arrays, performance can be crucial, especially in demanding applications. Lodash's _.each method is often faster than the native forEach function. This article explains why _.each tends to outperform forEach by exploring the design choices and optimizations behind it. We'll compare their performance, examine
2 min read
Lodash _.compact() Function
Lodash _.compact() function is used to create an array with all false values removed in JavaScript. so it returns a new array of filtered values. Syntax:_.compact(array);Parameters:array: It is an array to be compacted.Note: The values are false, null, 0, "", undefined, and NaN are falsey. Return Value: This function returns the array after filteri
3 min read
Lodash _.difference() Function
Lodash _.difference() function is used to remove a single element or the array of elements from the original array. This function works pretty much the same as the core function of JavaScript i.e. filter. Syntax:_.difference(array, [values]);Parameters:array: It is the array from which different elements are to be removed.values: It is the array of
2 min read
Lodash _.dropRightWhile() Function
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.dropRightWhile() function is used to delete the slice of the array excluding elements dropped from the end until the predicate function returns false. Syntax:  _.dropRightWhile(array, [predicate=_.identit
3 min read
Lodash _.dropRight() Function
Lodash _.dropRight() function is used to delete the elements from the right of the array i.e from the (n-1)th element. Syntax: _.dropRight(array, n);Parameters:array: It is the original array from which elements are to be deleted.n: Here n is the number of elements that are to be deleted from the array. By default, it is set to one.Return Value: It
3 min read
Lodash _.findLastIndex() Function
Lodash _.findLastIndex() function is used to find the element from the right of the array. Therefore giving the index of the last occurrence of the element in the array. Syntax:findLastIndex(array, [predicate=_.identity], fromIndex);Parameters:array: It is the original array.predicate: It is the function that iterates over each element.fromIndex: I
3 min read
Lodash _.initial() Function
Lodash _.initial() function is used to get all elements of the array in the range 0 to n-2. Thereby getting all elements except the (n-1)th element. Syntax: Lodash.initial(arrayObject);Parameters:arrayObject: There is only one parameter of type array.Return Value: It returns an array object.Example 1: In this example, When an array of objects is gi
3 min read
Lodash _.reverse() Function
Lodash _.reverse() is used to reverse the array. This function changes the original array and also returns a new array. Syntax: _.reverse( array )Parameters: This function accepts a single parameter array that holds the array of elements.Return Value: It returns the reversed array.Note: Please install lodash module by using npm install lodash befor
2 min read
Lodash _.slice() Function
Lodash _.slice() function is used to take a slice of the array from the starting index to the end index here end index is exclusive and the start index is inclusive. Syntax: _.slice(array, startIndex, endIndex);Parameters:array: It is the array from which the slice is to be taken.startIndex: It is the starting index from which slicing of the array
3 min read
Lodash _.tail() Function
The _.tail() method is used to create a new array that doesn't contain the first element of the original array. Syntax:_.tail(array);Parameters:array: This parameter holds the query array.Return Value:This method returns the slice of an array. Example 1: In this example, we are getting the tail of the array by the use of the lodash _.tail() method.
2 min read
Lodash _.isNumber() Function
Lodash _.isNumber() method is used to find whether the given value parameter is a number or not. If the given value is a number then it returns True value otherwise, it returns False. Syntax:_.isNumber(value); Parameters:value: This parameter holds the value to check.Return Value: This method returns true if the value is a number, else false.Exampl
2 min read
How To Fix Error "debounce not working in anonymous function" in Lodash?
When working with high-frequency events like scroll, resize, or input in JavaScript, debouncing helps to improve performance by limiting the rate at which the event handler is invoked. Lodash provides a debounce() function that delays the execution of a function until a specified amount of time has passed. However, many developers face the issue of
4 min read
Currying And Function Composition Using Lodash Flow
Currying and Function composition are essential concepts in functional programming that enhance code reusability and readability. Currying transforms a function with multiple parameters into a series of functions, each taking a single parameter.Function composition allows us to combine multiple functions into one, where the output of one function b
3 min read
How to use 'lodash' Package for Array Manipulation in a JavaScript Project ?
In this article, we'll look at how to use lodash for array manipulation tasks like sorting, filtering, mapping, etc. The 'lodash' is a very important and useful npm package that provides a set of functions to work with arrays, strings, objects, etc. Working with arrays is an important task in a javascript project which can be done through the 'loda
4 min read