The lodash method remove exported as a module for removing array elements matching a predicate
npx @tessl/cli install tessl/npm-lodash--remove@4.7.0Lodash 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.
npm install lodash.removeconst remove = require('lodash.remove');For ES modules (if available):
import remove from 'lodash.remove';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 returnedRemoves 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:
(value, index, array) => boolean[path, value] for matchingReturns:
Key Characteristics:
(value, index, array)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: