Skip to content

Function: toggleArrayItem() ​

ts
function toggleArrayItem<T>(arr, item): T[];

Toggles an item in an array: removes it if present, otherwise appends it. 切换数组中某个元素的选中状态:存在则移除,不存在则追加。

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterTypeDescription
arrreadonly T[]Source array / 源数组
itemTItem to toggle / 要切换的元素

Returns ​

T[]

New array / 新数组

Example ​

ts
toggleArrayItem([1, 2, 3], 2) // => [1, 3]
toggleArrayItem([1, 3], 2) // => [1, 3, 2]

Since ​

1.5.0

Released under the MIT License.