Merge Multiple Arrays and Strings into Single Array using Lodash

In this blog, we will learn how to merge multiple arrays and strings into a single array with Lodash library. We can merge or combine multiple arrays and strings into a single array with Lodash by using the _.concat() method provide by the Lodash library. This method joins two or more strings and it doesn’t change the existing strings/arrays. It returns a new string/array. In that way our original data is not changed.

Merge Multiple Array & String Into One Array

Syntax

_.concat(string1, string2, …, stringN)

Concatenation with Lodash

const _ = require("lodash");

const str = 70;
const arr1 = [20];
const arr2 = [40, 35];
const arr3 = [[20]]

const final_arr = _.concat(str, arr1, arr2, arr3);

Output

[70,20,40,35,[20]]

The _.concat() method takes all the arrays and other items as parameters and combines them together to generate the output (that’s a new array).

Concatenation with JS

<script>
let text1 = "Hello";
let text2 = "world!";
let text3 = "Have a nice day!";
let result = text1.concat(" ", text2, " ", text3);
console.log(result);
</script>

Hope you have understood how to concatenate two strings/arrays in JavaScript. Any suggestions are welcomed in the comment section.

Leave a Comment