The lodash method reject exported as a module for filtering collections.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The lodash.reject module provides the reject method from the lodash utility library as a standalone package. This method creates an array of elements that a predicate function returns falsy for, effectively filtering out elements that match the provided criteria.
npm install lodash.rejectconst reject = require('lodash.reject');ES6 module:
import reject from 'lodash.reject';const reject = require('lodash.reject');
const users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true }
];
// Reject based on function predicate
const activeUsers = reject(users, function(o) { return !o.active; });
// => [{ 'user': 'fred', 'age': 40, 'active': true }]
// Reject using object predicate (matches properties)
const nonMatching = reject(users, { 'age': 40, 'active': true });
// => [{ 'user': 'barney', 'age': 36, 'active': false }]
// Reject using property-value pair predicate
const notInactive = reject(users, ['active', false]);
// => [{ 'user': 'fred', 'age': 40, 'active': true }]
// Reject using property name predicate
const inactive = reject(users, 'active');
// => [{ 'user': 'barney', 'age': 36, 'active': false }]Creates an array of elements predicate returns falsy for. The opposite of _.filter; this method returns the elements of collection that predicate does NOT return truthy for.
/**
* Creates an array of elements predicate returns falsy for.
* @param {Array|Object} collection - The collection to iterate over.
* @param {Function|Object|string} [predicate=_.identity] - The function invoked per iteration.
* @returns {Array} Returns the new filtered array.
*/
function reject(collection, predicate);Parameters:
collection (Array|Object): The collection to iterate over - can be an array or objectpredicate (Function|Object|string, optional): The function invoked per iteration. Defaults to _.identityReturns:
Array: Returns the new filtered array containing elements that do NOT match the predicatePredicate Types:
Function: function(value, index, collection) => boolean
true to reject (exclude) the elementObject: { property: value, ... }
_.matches iteratee shorthandArray: [property, value]
object[property] === value_.matchesProperty iteratee shorthandString: 'property'
object[property] is truthy_.property iteratee shorthandUsage Examples:
const reject = require('lodash.reject');
// Function predicate - reject active users
const users = [
{ name: 'Alice', active: true },
{ name: 'Bob', active: false },
{ name: 'Charlie', active: true }
];
const inactiveUsers = reject(users, user => user.active);
// => [{ name: 'Bob', active: false }]
// Object predicate - reject exact matches
const products = [
{ category: 'electronics', price: 100 },
{ category: 'books', price: 20 },
{ category: 'electronics', price: 200 }
];
const nonElectronics = reject(products, { category: 'electronics' });
// => [{ category: 'books', price: 20 }]
// Array predicate - reject by property-value pair
const items = [
{ type: 'urgent', priority: 1 },
{ type: 'normal', priority: 2 },
{ type: 'low', priority: 3 }
];
const nonUrgent = reject(items, ['type', 'urgent']);
// => [{ type: 'normal', priority: 2 }, { type: 'low', priority: 3 }]
// String predicate - reject by truthy property
const tasks = [
{ title: 'Task 1', completed: true },
{ title: 'Task 2', completed: false },
{ title: 'Task 3', completed: true }
];
const incompleteTasks = reject(tasks, 'completed');
// => [{ title: 'Task 2', completed: false }]
// With arrays of primitives
const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = reject(numbers, n => n % 2 === 0);
// => [1, 3, 5]
// With objects (iterates over object values)
const scores = { alice: 85, bob: 92, charlie: 78 };
const belowNinety = reject(scores, score => score >= 90);
// => [85, 78]arrayFilter for arrays and baseFilter for objectsgetIteratee(predicate, 3) to handle different predicate types_.matches, _.matchesProperty, _.property)