Aller au contenu principal

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.

remarque

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']

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]