Lodash _.concat() method
What you’ll learn
- How
_.concat(array, ...values)flattens arguments one level into a new array. - When nested arrays stay nested versus when their elements are spliced in.
- How to import
concatand compare with the spread operator. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN. - Shallow-copy pitfalls when mutating shared objects.
Prerequisites
Arrays and rest/spread syntax help; optional prior pages _.chunk() and _.compact().
- You know that
[1, 2].concat([3])in native JavaScript produces a new array. - You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.concat builds a new array starting from the first argument, then appends every following value. If a following value is itself an array, its elements are appended in order (same rule as Array.prototype.concat).
Non-destructive
The first array is never mutated; you always receive a fresh array instance.
Combine lists
Typical for merging base rows with extra IDs, tags, or route segments.
Shallow only
Nested objects or arrays are not cloned; references are shared with the sources.
Syntax
_.concat(array, [values]) - array: starting collection (array-like values are coerced).
- values: any number of additional values or arrays; arrays contribute their elements.
- Returns: a new concatenated array.
Primitives and arrays
Numbers and array arguments are flattened one level into the result.
import concat from "lodash/concat";
const merged = concat([1], 2, [3]);
// [1, 2, 3] Nested array preserved
Passing [[2]] appends that array’s only element, which is the inner array [2].
import concat from "lodash/concat";
concat([1], [[2]]);
// [1, [2]] Empty start
You can begin from an empty array and only append later arguments.
import concat from "lodash/concat";
concat([], [1, 2], 3);
// [1, 2, 3] 📋 _.concat vs spread
| Approach | When it shines | Trade-off |
|---|---|---|
_.concat(a, b, c) | Variadic helper with consistent Lodash coercion | Extra import in modern bundles |
[...a, ...b, c] | Native, terse, great with typed pipelines | Spread only works on iterables; mental model differs for nested arrays |
Pitfalls to avoid
Shared object references
If you mutate an object that still lives inside the original array, the concatenated array sees the same change.
Expecting deep merge
concat is not merge or union; it only sequences elements. Nested structures are not recursively flattened.
Order matters
Arguments are appended left to right; swapping arrays changes the final ordering of duplicates.
❓ FAQ
Summary
- Purpose:
_.concat(array, ...values)returns one new array with every argument flattened one level. - Safety: the first array is left unchanged.
- Next: Lodash _.difference(), _.compact(), or the array methods hub.
_.concat does not mutate the first array. Arguments that are arrays contribute their elements (one level); a nested array like [[2]] contributes a single element [2] to the result.
6 people found this page helpful
