how to use the concat function from lodash in javascript

To use the concat function from lodash, first install it via npm:

index.tsx
npm install lodash
19 chars
2 lines

Then, import it into your Javascript file:

index.tsx
const _ = require('lodash');
29 chars
2 lines

To concatenate two or more arrays using lodash.concat, you can do the following:

index.tsx
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = [7, 8, 9];

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

console.log(result); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
169 chars
8 lines

You can also use lodash.concat to concatenate arrays with other values, like so:

index.tsx
const arr1 = [1, 2, 3];
const val = 4;
const str = 'hello';

const result = _.concat(arr1, val, str);

console.log(result); // [1, 2, 3, 4, 'hello']
149 chars
8 lines

In addition to arrays, lodash.concat can be used to concatenate other Javascript data types, including objects and strings.

gistlibby LogSnag