swapRemove

View sourceswapRemove<T>(arr: T[], index: number): void

Removes an item by replacing it with the array’s final item, then popping the final item. Use this when the array’s order does not matter.

An index of -1 is treated as a no-op. Other indexes are not validated. If the removed item is not the final item and the final item is falsy, the final item is still popped but the target slot is left unchanged because the implementation uses a truthiness check.

Examples

const values = ['a', 'b', 'c']
swapRemove(values, 0)
// values is ['c', 'b']

Type Parameters

T

Parameters

NameDescriptionDefault
arr

Array to modify in place.

—
index

Index to remove, or -1 to leave the array unchanged.

—

Returns

Nothing. The supplied array is modified in place.