Creates a new array concatenating array with any additional arrays and/or values
npx @tessl/cli install tessl/npm-lodash--concat@4.5.0Lodash concat creates a new array concatenating an array with any additional arrays and/or values. This function provides a reliable way to combine arrays without mutating the original array, handling edge cases gracefully.
npm install lodash (concat is part of the main lodash library)import { concat } from "lodash";For CommonJS:
const { concat } = require("lodash");Alternative global usage:
// If lodash is loaded globally
_.concat(array, ...values);import { concat } from "lodash";
// Basic concatenation
const array = [1];
const result = concat(array, 2, [3], [[4]]);
console.log(result);
// => [1, 2, 3, [4]]
// Original array is unchanged
console.log(array);
// => [1]
// Handling null/undefined input
const nullResult = concat(null, 1, 2);
console.log(nullResult);
// => [1, 2]
const undefinedResult = concat(undefined, 1, [2], [[3]]);
console.log(undefinedResult);
// => [1, 2, [3]]
// Non-array first parameter
const stringResult = concat("hello", " ", "world");
console.log(stringResult);
// => ["hello", " ", "world"]Creates a new array by combining the input array with additional values, handling non-array inputs gracefully.
/**
* Creates a new array concatenating array with any additional arrays and/or values.
* @param {Array} array - The array to concatenate
* @param {...*} [values] - The values to concatenate
* @returns {Array} Returns the new concatenated array
*/
function concat(array, ...values);Behavior Details:
array is not an array:
null or undefined becomes an empty array [][Object(value)]Usage Examples:
import { concat } from "lodash";
// Multiple arrays and values
const arr1 = [1, 2];
const arr2 = [3, 4];
const result = concat(arr1, arr2, 5, [6, 7]);
console.log(result);
// => [1, 2, 3, 4, 5, 6, 7]
// Nested array handling (one level flattening)
const nested = concat([1], [[2, 3]], [[[4]]]);
console.log(nested);
// => [1, [2, 3], [[4]]]
// Empty array concatenation
const empty = concat([], 1, 2, 3);
console.log(empty);
// => [1, 2, 3]
// Mixed data types
const mixed = concat([1, 2], "hello", true, { key: "value" });
console.log(mixed);
// => [1, 2, "hello", true, { key: "value" }]
// Sparse arrays (treated as dense)
const sparse = concat(Array(2), Array(1));
console.log(sparse);
// => [undefined, undefined, undefined]
console.log('0' in sparse); // => true