replaceOrAppend
替換數組中匹配條件的第一個元素,如果沒有找到匹配的元素則追加到數組末尾。
基礎用法
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 }, // 新數據
user => user.id === 2 // 匹配條件
)
// [
// { id: 1, name: 'Alice', age: 25 },
// { id: 2, name: 'Bob', age: 31 }, // 已更新
// { id: 3, name: 'Charlie', age: 35 }
// ]
語法
typescript
function replaceOrAppend<T>(
array: readonly T[],
item: T,
matcher: (item: T, index: number, array: readonly T[]) => boolean
): T[]
參數
array
(readonly T[]): 要操作的數組item
(T): 要替換或追加的元素matcher
(function): 用於匹配元素的函數item
(T): 當前元素index
(number): 當前元素的索引array
(readonly T[]): 原數組
返回值
返回一個新數組,包含替換後的元素或追加的新元素。
示例
替換現有元素
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 }, // 降價了
product => product.id === 2
)
// [
// { id: 1, name: 'Laptop', price: 999 },
// { id: 2, name: 'Phone', price: 549 }, // 已更新
// { id: 3, name: 'Tablet', price: 399 }
// ]
追加新元素
typescript
import { replaceOrAppend } from 'radash'
const fruits = ['apple', 'banana', 'cherry']
const updatedFruits = replaceOrAppend(
fruits,
'orange',
fruit => fruit === 'grape' // 找不到grape
)
// ['apple', 'banana', 'cherry', 'orange'] // orange被追加到末尾
更新用戶信息
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' } // 郵箱已更新
// ]
處理數字數組
typescript
import { replaceOrAppend } from 'radash'
const numbers = [1, 2, 3, 4, 5]
// 替換第一個偶數
const updatedNumbers = replaceOrAppend(
numbers,
10,
num => num % 2 === 0
)
// [1, 10, 3, 4, 5] // 第一個偶數2被替換為10
// 替換不存在的元素
const newNumbers = replaceOrAppend(
numbers,
100,
num => num === 999 // 找不到999
)
// [1, 2, 3, 4, 5, 100] // 100被追加到末尾
處理字符串數組
typescript
import { replaceOrAppend } from 'radash'
const words = ['hello', 'world', 'javascript']
const updatedWords = replaceOrAppend(
words,
'typescript',
word => word === 'javascript'
)
// ['hello', 'world', 'typescript'] // javascript被替換為typescript
使用索引匹配
typescript
import { replaceOrAppend } from 'radash'
const items = ['a', 'b', 'c', 'd']
const updatedItems = replaceOrAppend(
items,
'x',
(_, index) => index === 1 // 替換索引為1的元素
)
// ['a', 'x', 'c', 'd'] // 'b'被替換為'x'
注意事項
- 保持原數組不變:
replaceOrAppend
不會修改原數組,而是返回新的數組 - 只替換第一個匹配: 如果有多個匹配的元素,只替換第一個
- 追加到末尾: 如果沒有找到匹配的元素,新元素會被追加到數組末尾
- 性能: 時間復雜度為 O(n),其中 n 是數組長度
- 匹配函數: 匹配函數應該返回布爾值
與其他方法的區別
map()
: 會處理所有元素,而不是只替換第一個匹配的filter()
: 只過濾元素,不能替換replaceOrAppend()
: 專門用於替換或追加操作splice()
: 會修改原數組,而replaceOrAppend
返回新數組
實際應用場景
- 用戶管理: 更新用戶信息
- 商品管理: 更新商品價格或信息
- 配置管理: 更新配置項
- 表單處理: 更新表單數據
- 緩存管理: 更新緩存項