Skip to main content

remove()

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

Removes all elements from array that predicate returns truthy for.

DEPRECATED

Use .filter() for immutable operations.

Reason:
Pithos design philosophy always favors immutability.

note

Mutates the array in place.


Type Parametersโ€‹

T: Tโ€‹

The type of elements in the array.


Parametersโ€‹

array: T[]โ€‹

The array to modify.

predicate: (value, index, array) => booleanโ€‹

The function invoked per iteration.


Returns: T[]โ€‹

An array of removed elements.


Sinceโ€‹

2.0.0


Also known asโ€‹

remove (Lodash, es-toolkit, Antfu) ยท โŒ (Remeda, Radashi, Ramda, Effect, Modern Dash)


Exampleโ€‹

// โŒ Deprecated approach (mutates array)
const array = [1, 2, 3, 4];
const evens = remove(array, n => n % 2 === 0);
console.log(evens); // [2, 4]
console.log(array); // [1, 3]

// โœ… Recommended approach (immutable)
const array = [1, 2, 3, 4];
const evens = array.filter(n => n % 2 === 0);
const odds = array.filter(n => n % 2 !== 0);
console.log(evens); // [2, 4]
console.log(odds); // [1, 3]
console.log(array); // [1, 2, 3, 4] (unchanged)

How it works?โ€‹

Removes elements matching predicate and returns them (mutates).

Processing Flowโ€‹

Common Inputsโ€‹

ArrayPredicateRemovedRemaining
[1, 2, 3, 4]n => n % 2 === 0[2, 4][1, 3]
['a', '', 'b']x => !x['']['a', 'b']

warning Deprecated: Use .filter() for immutable operations.


Use Casesโ€‹

Remove matching elements ๐Ÿ“Œโ€‹

Remove elements that match a condition.

const items = [1, 2, 3, 4, 5, 6];
items.filter(n => n % 2 !== 0);
// => [1, 3, 5] (odd numbers only)

Extract and removeโ€‹

Separate matching and non-matching elements.

const users = [{ active: true }, { active: false }, { active: true }];
const active = users.filter(u => u.active);
const inactive = users.filter(u => !u.active);

Remove by conditionโ€‹

Clean array by removing unwanted items.

const data = [null, 1, undefined, 2, "", 3];
data.filter(x => x != null && x !== "");
// => [1, 2, 3]