or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--remove

The lodash method remove exported as a module for removing array elements matching a predicate

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.remove@4.7.x

To install, run

npx @tessl/cli install tessl/npm-lodash--remove@4.7.0

index.mddocs/

Lodash Remove

Lodash Remove provides the remove function for mutating arrays by removing elements that match a predicate function. This standalone module exports the same functionality as lodash's _.remove() method, enabling selective array element removal while preserving the original array structure.

Package Information

  • Package Name: lodash.remove
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.remove

Core Imports

const remove = require('lodash.remove');

For ES modules (if available):

import remove from 'lodash.remove';

Basic Usage

const remove = require('lodash.remove');

const array = [1, 2, 3, 4, 5, 6];
const evens = remove(array, function(n) {
  return n % 2 === 0;
});

console.log(array);  // [1, 3, 5] - original array is mutated
console.log(evens);  // [2, 4, 6] - removed elements are returned

Capabilities

Array Element Removal

Removes all elements from an array that match a predicate function and returns the removed elements. The original array is mutated in place.

/**
 * Removes all elements from array that predicate returns truthy for
 * and returns an array of the removed elements. The predicate is invoked
 * with three arguments: (value, index, array).
 * 
 * @param {Array} array - The array to modify
 * @param {Array|Function|Object|string} [predicate=_.identity] - The function invoked per iteration
 * @returns {Array} Returns the new array of removed elements
 */
function remove(array, predicate);

Parameters:

  • array (Array): The array to modify. Must be an array-like object that will be mutated.
  • predicate (Array|Function|Object|string, optional): The function invoked per iteration. Supports multiple formats:
    • Function: Custom predicate (value, index, array) => boolean
    • Object: Matches object properties (deep equal comparison)
    • Array: Property-value pair [path, value] for matching
    • String: Property path for truthy check

Returns:

  • (Array): New array containing all removed elements in the order they were found

Key Characteristics:

  • Mutating Operation: Modifies the original array in place
  • Safe Iteration: Uses two-pass algorithm to avoid index shifting issues
  • Predicate Arguments: Function predicates receive (value, index, array)
  • Shorthand Support: Supports lodash iteratee shorthands for common patterns
  • Empty Array Handling: Returns empty array if input is empty, null, or undefined

Usage Examples:

const remove = require('lodash.remove');

// Function predicate
const numbers = [1, 2, 3, 4, 5];
const evens = remove(numbers, function(n) {
  return n % 2 === 0;
});
// numbers: [1, 3, 5], evens: [2, 4]

// Object matching shorthand
const users = [
  { name: 'Alice', active: true },
  { name: 'Bob', active: false },
  { name: 'Carol', active: true }
];
const inactive = remove(users, { active: false });
// users: [{ name: 'Alice', active: true }, { name: 'Carol', active: true }]
// inactive: [{ name: 'Bob', active: false }]

// Property path shorthand
const items = [
  { id: 1, archived: false },
  { id: 2, archived: true },
  { id: 3, archived: false }
];
const archived = remove(items, 'archived');
// items: [{ id: 1, archived: false }, { id: 3, archived: false }]
// archived: [{ id: 2, archived: true }]

// Array shorthand (property-value pair)
const products = [
  { name: 'Laptop', category: 'electronics' },
  { name: 'Book', category: 'books' },
  { name: 'Phone', category: 'electronics' }
];
const electronics = remove(products, ['category', 'electronics']);
// products: [{ name: 'Book', category: 'books' }]
// electronics: [{ name: 'Laptop', category: 'electronics' }, { name: 'Phone', category: 'electronics' }]

// Predicate with index and array arguments
const data = ['a', 'b', 'c', 'd'];
const removed = remove(data, function(value, index, array) {
  console.log(`Checking ${value} at index ${index} in array of length ${array.length}`);
  return index % 2 === 1; // Remove elements at odd indices
});
// data: ['a', 'c'], removed: ['b', 'd']

Error Handling:

The function handles edge cases gracefully:

  • Returns empty array if input array is null, undefined, or empty
  • Skips over array holes and sparse arrays
  • Predicate functions that throw errors will propagate the error