remove

array

Removes all elements from array that predicate returns truthy for and returns an array of the removed elements.

Signature

remove<T>(array: T[], predicate: (value: T, index: number, array: T[]) => boolean): T[]

Parameters

NameTypeDescription
arrayT[]The array to modify
predicate(value: T, index: number, array: T[]) => booleanThe function invoked per iteration

Returns

T[] - Returns the new array of removed elements

Examples

Remove even numbers

import { remove } from 'dashlite'

const array = [1, 2, 3, 4]
const evens = remove(array, (n) => n % 2 === 0)
console.log(array)
console.log(evens)

Output:

[1, 3] [2, 4]

Notes

  • This method mutates array

Available since version 1.0.0