GuobaGuoba Utils

remove

@guoba-ai/utils / array / remove

Function: remove()

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

Defined in: array.ts:179

Remove elements from an array in-place that match the predicate.

Parameters

array

T[]

The array to modify

predicate

(v) => boolean

Function that returns true for elements to remove

Returns

T[]

An array of removed elements

Example

const arr = [1, 2, 3, 4, 5]
remove(arr, v => v % 2 === 0) // [2, 4]
// arr is now [1, 3, 5]

remove(arr, v => v > 10) // []

const users = [{ active: true }, { active: false }]
remove(users, user => !user.active) // [{ active: false }]

Warning

This function mutates the input array and returns the removed elements.

On this page