Skip to content

@pmun/utils / remove

Function: remove()

ts
function remove<T>(array, item): T[];

从数组中移除指定的项

Type Parameters

T

T

Parameters

array

T[]

原始数组

item

T

要移除的项

Returns

T[]

移除指定项后的新数组,不会修改原始数组

Example

ts
const numbers = [1, 2, 3, 4, 5]
const newArray = remove(numbers, 3) // [1, 2, 4, 5]
console.log(numbers) // 原数组不变: [1, 2, 3, 4, 5]

// 移除字符串
const fruits = ['苹果', '香蕉', '橙子']
const newFruits = remove(fruits, '香蕉') // ['苹果', '橙子']