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);