concat()
concat<
T>(array, ...values):T[]
Concatenates arrays and values into a new array.
DEPRECATED
Use array.concat() directly instead.
Reason:
Native equivalent method now available
Type Parametersโ
T: Tโ
The type of elements in the arrays.
Parametersโ
array: T[]โ
The initial array.
values: ...(T | T[])[]โ
Additional arrays or values to concatenate.
Returns: T[]โ
A new array containing all elements from the input arrays.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
appendAll (Effect) ยท concat (Lodash, es-toolkit, Remeda, Ramda) ยท mergeArrayable (Antfu) ยท โ (Radashi, Modern Dash)
Exampleโ
const arr1 = [1, 2];
const arr2 = [3, 4];
// โ Deprecated approach
const concatenated = concat(arr1, arr2);
console.log(concatenated); // [1, 2, 3, 4]
// โ
Recommended approach
const concatenatedNative = arr1.concat(arr2);
console.log(concatenatedNative); // [1, 2, 3, 4]
How it works?โ
Creates a new array concatenating arrays/values.
Deprecated: Use array.concat() or spread operator directly.
Native Equivalentโ
// โ concat(arr, 2, [3])
// โ
arr.concat(2, [3])
// โ
[...arr, 2, ...other]
Use Casesโ
Merge multiple data sources ๐โ
Combine arrays from different sources into a single collection.
const local = [{ id: 1 }];
const remote = [{ id: 2 }, { id: 3 }];
local.concat(remote);
// => [{ id: 1 }, { id: 2 }, { id: 3 }]
Append new items to list immutablyโ
Add new items without mutation.
const todos = [{ id: 1, text: "Buy groceries" }];
const newTodo = { id: 2, text: "Walk the dog" };
todos.concat([newTodo]);
Prepend default options to user choicesโ
Add default options at the beginning of a list.
const defaultOption = [{ value: "", label: "Select..." }];
const countries = [{ value: "us", label: "USA" }];
defaultOption.concat(countries);