replace
替换数组中匹配条件的第一个元素。
基础用法
typescript
import { replace } from 'radash'
const users = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 }
]
const updatedUsers = replace(
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 replace<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 { replace } from 'radash'
const products = [
{ id: 1, name: 'Laptop', price: 999 },
{ id: 2, name: 'Phone', price: 599 },
{ id: 3, name: 'Tablet', price: 399 }
]
const updatedProducts = replace(
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 { replace } from 'radash'
const numbers = [1, 2, 3, 4, 5]
const updatedNumbers = replace(numbers, 10, num => num === 3)
// [1, 2, 10, 4, 5] // 3被替换为10
const words = ['hello', 'world', 'javascript']
const updatedWords = replace(words, 'typescript', word => word === 'javascript')
// ['hello', 'world', 'typescript'] // javascript被替换为typescript
使用索引匹配
typescript
import { replace } from 'radash'
const items = ['a', 'b', 'c', 'd']
const updatedItems = replace(items, 'x', (_, index) => index === 1)
// ['a', 'x', 'c', 'd'] // 索引1的元素被替换为'x'
替换不存在的元素
typescript
import { replace } from 'radash'
const fruits = ['apple', 'banana', 'cherry']
const updatedFruits = replace(fruits, 'orange', fruit => fruit === 'grape')
// ['apple', 'banana', 'cherry'] // 没有找到grape,返回原数组
替换对象数组中的元素
typescript
import { replace } from 'radash'
const users = [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com' }
]
const updatedUsers = replace(
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' }, // 邮箱已更新
// { id: 3, name: 'Charlie', email: 'charlie@example.com' }
// ]
替换嵌套对象
typescript
import { replace } from 'radash'
const config = {
settings: {
theme: 'dark',
language: 'en',
notifications: true
},
users: [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]
}
const updatedConfig = replace(
config.users,
{ id: 2, name: 'Bob Updated' },
user => user.id === 2
)
// 替换了users数组中的Bob对象
处理条件替换
typescript
import { replace } from 'radash'
const scores = [85, 92, 78, 96, 88]
// 替换第一个低于80的分数
const updatedScores = replace(scores, 80, score => score < 80)
// [85, 92, 80, 96, 88] // 78被替换为80
// 替换第一个偶数
const numbers = [1, 3, 4, 7, 9]
const updatedNumbers = replace(numbers, 0, num => num % 2 === 0)
// [1, 3, 0, 7, 9] // 4被替换为0
在状态管理中使用
typescript
import { replace } from 'radash'
interface Todo {
id: number
text: string
completed: boolean
}
const todos: Todo[] = [
{ id: 1, text: 'Learn React', completed: false },
{ id: 2, text: 'Build app', completed: false },
{ id: 3, text: 'Deploy', completed: false }
]
// 标记任务为完成
const toggleTodo = (todoId: number) => {
return replace(todos,
{ id: todoId, text: 'Learn React', completed: true },
todo => todo.id === todoId
)
}
const updatedTodos = toggleTodo(1)
// 第一个任务被标记为完成
处理数组中的重复元素
typescript
import { replace } from 'radash'
const items = ['apple', 'banana', 'apple', 'cherry']
// 替换第一个'apple'
const updatedItems = replace(items, 'orange', item => item === 'apple')
// ['orange', 'banana', 'apple', 'cherry'] // 只替换第一个apple
注意事项
- 保持原数组不变:
replace
不会修改原数组,而是返回新的数组 - 只替换第一个匹配: 如果有多个匹配的元素,只替换第一个
- 未找到匹配: 如果没有找到匹配的元素,返回原数组
- 性能: 时间复杂度为 O(n),其中 n 是数组长度
- 匹配函数: 匹配函数应该返回布尔值
与其他方法的区别
map()
: 会处理所有元素,而不是只替换第一个匹配的filter()
: 只过滤元素,不能替换replace()
: 专门用于替换操作splice()
: 会修改原数组,而replace
返回新数组replaceOrAppend()
: 如果没找到匹配会追加,而replace
只替换
实际应用场景
- 用户管理: 更新用户信息
- 商品管理: 更新商品价格或信息
- 配置管理: 更新配置项
- 表单处理: 更新表单数据
- 状态管理: 在React等框架中更新状态