Skip to content

removeItemsBy

Removes all items from an array that match a predicate function, mutating the original array, and returns the removed items as a new array.

Usage

ts
import { removeItemsBy } from 'rattail'

const arr = [1, 2, 3, 4]
const removed = removeItemsBy(arr, (v) => v % 2 === 0)
// arr becomes [1, 3]
// removed is [2, 4]

const users = [
  { id: 1, name: 'A' },
  { id: 2, name: 'B' },
  { id: 3, name: 'C' },
]
const removedUsers = removeItemsBy(users, (u) => u.id === 2)
// users becomes [{ id: 1, name: 'A' }, { id: 3, name: 'C' }]
// removedUsers is [{ id: 2, name: 'B' }]

Arguments

ArgTypeDefaults
arrArray
fn(item) => any

Return

Type
Array

Notes

  • The original array is mutated in place.
  • All items matching the predicate will be removed and returned as a new array.