Skip to content

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'

注意事項

  1. 保持原數組不變: replaceOrAppend 不會修改原數組,而是返回新的數組
  2. 只替換第一個匹配: 如果有多個匹配的元素,只替換第一個
  3. 追加到末尾: 如果沒有找到匹配的元素,新元素會被追加到數組末尾
  4. 性能: 時間復雜度為 O(n),其中 n 是數組長度
  5. 匹配函數: 匹配函數應該返回布爾值

與其他方法的區別

  • map(): 會處理所有元素,而不是只替換第一個匹配的
  • filter(): 只過濾元素,不能替換
  • replaceOrAppend(): 專門用於替換或追加操作
  • splice(): 會修改原數組,而 replaceOrAppend 返回新數組

實際應用場景

  1. 用戶管理: 更新用戶信息
  2. 商品管理: 更新商品價格或信息
  3. 配置管理: 更新配置項
  4. 表單處理: 更新表單數據
  5. 緩存管理: 更新緩存項

Released under the MIT License.