replaceOrAppend
Replace the first element in an array that matches the condition, or append to the end of the array if no matching element is found.
Basic Usage
typescript
import { replaceOrAppend } from 'radash'
const users = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 }
]
const updatedUsers = replaceOrAppend(
users,
{ id: 2, name: 'Bob', age: 31 }, // new data
user => user.id === 2 // matching condition
)
// [
// { id: 1, name: 'Alice', age: 25 },
// { id: 2, name: 'Bob', age: 31 }, // updated
// { id: 3, name: 'Charlie', age: 35 }
// ]
Syntax
typescript
function replaceOrAppend<T>(
array: readonly T[],
item: T,
matcher: (item: T, index: number, array: readonly T[]) => boolean
): T[]
Parameters
array
(readonly T[]): The array to operate onitem
(T): The element to replace or appendmatcher
(function): Function to match elementsitem
(T): Current elementindex
(number): Index of the current elementarray
(readonly T[]): The original array
Return Value
Returns a new array containing the replaced element or the appended new element.
Examples
Replacing Existing Elements
typescript
import { replaceOrAppend } from 'radash'
const products = [
{ id: 1, name: 'Laptop', price: 999 },
{ id: 2, name: 'Phone', price: 599 },
{ id: 3, name: 'Tablet', price: 399 }
]
const updatedProducts = replaceOrAppend(
products,
{ id: 2, name: 'Phone', price: 549 }, // price reduced
product => product.id === 2
)
// [
// { id: 1, name: 'Laptop', price: 999 },
// { id: 2, name: 'Phone', price: 549 }, // updated
// { id: 3, name: 'Tablet', price: 399 }
// ]
Appending New Elements
typescript
import { replaceOrAppend } from 'radash'
const fruits = ['apple', 'banana', 'cherry']
const updatedFruits = replaceOrAppend(
fruits,
'orange',
fruit => fruit === 'grape' // grape not found
)
// ['apple', 'banana', 'cherry', 'orange'] // orange appended to the end
Updating User Information
typescript
import { replaceOrAppend } from 'radash'
const users = [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' }
]
const updatedUsers = replaceOrAppend(
users,
{ id: 2, name: 'Bob', email: 'bob.new@example.com' },
user => user.id === 2
)
// [
// { id: 1, name: 'Alice', email: 'alice@example.com' },
// { id: 2, name: 'Bob', email: 'bob.new@example.com' } // email updated
// ]
Handling Number Arrays
typescript
import { replaceOrAppend } from 'radash'
const numbers = [1, 2, 3, 4, 5]
// Replace the first even number
const updatedNumbers = replaceOrAppend(
numbers,
10,
num => num % 2 === 0
)
// [1, 10, 3, 4, 5] // first even number 2 replaced with 10
// Replace non-existent element
const newNumbers = replaceOrAppend(
numbers,
100,
num => num === 999 // 999 not found
)
// [1, 2, 3, 4, 5, 100] // 100 appended to the end
Handling String Arrays
typescript
import { replaceOrAppend } from 'radash'
const words = ['hello', 'world', 'javascript']
const updatedWords = replaceOrAppend(
words,
'typescript',
word => word === 'javascript'
)
// ['hello', 'world', 'typescript'] // javascript replaced with typescript
Using Index Matching
typescript
import { replaceOrAppend } from 'radash'
const items = ['a', 'b', 'c', 'd']
const updatedItems = replaceOrAppend(
items,
'x',
(_, index) => index === 1 // replace element at index 1
)
// ['a', 'x', 'c', 'd'] // 'b' replaced with 'x'
Notes
- Keep original array unchanged:
replaceOrAppend
does not modify the original array, but returns a new array - Only replace first match: If there are multiple matching elements, only the first one is replaced
- Append to end: If no matching element is found, the new element is appended to the end of the array
- Performance: Time complexity is O(n), where n is the array length
- Matcher function: The matcher function should return a boolean value
Differences from Other Methods
map()
: Processes all elements, not just the first matching onefilter()
: Only filters elements, cannot replacereplaceOrAppend()
: Specifically designed for replace or append operationssplice()
: Modifies the original array, whilereplaceOrAppend
returns a new array
Practical Application Scenarios
- User management: Update user information
- Product management: Update product prices or information
- Configuration management: Update configuration items
- Form processing: Update form data
- Cache management: Update cache items