merge
Merge multiple arrays into one array, with optional deduplication.
Basic Usage
typescript
import { merge } from 'radash'
const array1 = [1, 2, 3]
const array2 = [4, 5, 6]
const array3 = [7, 8, 9]
const merged = merge(array1, array2, array3)
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
Syntax
typescript
function merge<T>(
...arrays: readonly T[][]
): T[]
Parameters
...arrays
(readonly T[][]): Arrays to merge, can pass any number of arrays
Return Value
Returns a merged array containing all elements from the input arrays.
Examples
Basic Merge
typescript
import { merge } from 'radash'
const fruits = ['apple', 'banana']
const vegetables = ['carrot', 'lettuce']
const grains = ['rice', 'wheat']
const allFood = merge(fruits, vegetables, grains)
// ['apple', 'banana', 'carrot', 'lettuce', 'rice', 'wheat']
Merging Object Arrays
typescript
import { merge } from 'radash'
const users1 = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]
const users2 = [
{ id: 3, name: 'Charlie' },
{ id: 4, name: 'Diana' }
]
const allUsers = merge(users1, users2)
// [
// { id: 1, name: 'Alice' },
// { id: 2, name: 'Bob' },
// { id: 3, name: 'Charlie' },
// { id: 4, name: 'Diana' }
// ]
Merging Different Types of Arrays
typescript
import { merge } from 'radash'
const numbers = [1, 2, 3]
const strings = ['a', 'b', 'c']
const booleans = [true, false]
const mixed = merge(numbers, strings, booleans)
// [1, 2, 3, 'a', 'b', 'c', true, false]
Merging Empty Arrays
typescript
import { merge } from 'radash'
const empty1: number[] = []
const empty2: number[] = []
const numbers = [1, 2, 3]
const result = merge(empty1, numbers, empty2)
// [1, 2, 3]
Merging Single Array
typescript
import { merge } from 'radash'
const numbers = [1, 2, 3]
const result = merge(numbers)
// [1, 2, 3]
Merging Nested Arrays
typescript
import { merge } from 'radash'
const array1 = [[1, 2], [3, 4]]
const array2 = [[5, 6], [7, 8]]
const merged = merge(array1, array2)
// [[1, 2], [3, 4], [5, 6], [7, 8]]
Comparison with Spread Operator
typescript
import { merge } from 'radash'
const array1 = [1, 2, 3]
const array2 = [4, 5, 6]
// Using merge
const merged1 = merge(array1, array2)
// [1, 2, 3, 4, 5, 6]
// Using spread operator
const merged2 = [...array1, ...array2]
// [1, 2, 3, 4, 5, 6]
Dynamic Merging
typescript
import { merge } from 'radash'
function combineArrays<T>(...arrays: T[][]) {
return merge(...arrays)
}
const result = combineArrays(
[1, 2],
[3, 4],
[5, 6]
)
// [1, 2, 3, 4, 5, 6]
Notes
- Keep original arrays unchanged:
merge
does not modify the original arrays, but returns a new array - Order: The merged array is arranged in the order of the input arrays
- Empty array handling: Empty arrays are ignored and do not affect the result
- Performance: Time complexity is O(n), where n is the total length of all arrays
- Memory: Will create a new array, pay attention to memory usage
Differences from Other Methods
concat()
: Can only merge two arrays, requires chaining callsspread operator
: Same functionality, but different syntaxmerge()
: More intuitive API provided by radash, supports multiple arrays
Practical Application Scenarios
- Data integration: Merge data from different sources
- Configuration merging: Merge multiple configuration arrays
- API responses: Merge results from multiple API calls
- File processing: Merge content from multiple files
- Cache management: Merge multiple cache arrays