0
# Lodash Reject
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: lodash.reject
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.reject`
10
11
## Core Imports
12
13
```javascript
14
const reject = require('lodash.reject');
15
```
16
17
ES6 module:
18
19
```javascript
20
import reject from 'lodash.reject';
21
```
22
23
## Basic Usage
24
25
```javascript
26
const reject = require('lodash.reject');
27
28
const users = [
29
{ 'user': 'barney', 'age': 36, 'active': false },
30
{ 'user': 'fred', 'age': 40, 'active': true }
31
];
32
33
// Reject based on function predicate
34
const activeUsers = reject(users, function(o) { return !o.active; });
35
// => [{ 'user': 'fred', 'age': 40, 'active': true }]
36
37
// Reject using object predicate (matches properties)
38
const nonMatching = reject(users, { 'age': 40, 'active': true });
39
// => [{ 'user': 'barney', 'age': 36, 'active': false }]
40
41
// Reject using property-value pair predicate
42
const notInactive = reject(users, ['active', false]);
43
// => [{ 'user': 'fred', 'age': 40, 'active': true }]
44
45
// Reject using property name predicate
46
const inactive = reject(users, 'active');
47
// => [{ 'user': 'barney', 'age': 36, 'active': false }]
48
```
49
50
## Capabilities
51
52
### Reject Function
53
54
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.
55
56
```javascript { .api }
57
/**
58
* Creates an array of elements predicate returns falsy for.
59
* @param {Array|Object} collection - The collection to iterate over.
60
* @param {Function|Object|string} [predicate=_.identity] - The function invoked per iteration.
61
* @returns {Array} Returns the new filtered array.
62
*/
63
function reject(collection, predicate);
64
```
65
66
**Parameters:**
67
68
- `collection` (Array|Object): The collection to iterate over - can be an array or object
69
- `predicate` (Function|Object|string, optional): The function invoked per iteration. Defaults to `_.identity`
70
71
**Returns:**
72
73
- `Array`: Returns the new filtered array containing elements that do NOT match the predicate
74
75
**Predicate Types:**
76
77
1. **Function**: `function(value, index, collection) => boolean`
78
- Receives the current value, index/key, and collection
79
- Return `true` to reject (exclude) the element
80
81
2. **Object**: `{ property: value, ... }`
82
- Matches objects with the same property values (partial deep comparison)
83
- Uses `_.matches` iteratee shorthand
84
85
3. **Array**: `[property, value]`
86
- Matches objects where `object[property] === value`
87
- Uses `_.matchesProperty` iteratee shorthand
88
89
4. **String**: `'property'`
90
- Matches objects where `object[property]` is truthy
91
- Uses `_.property` iteratee shorthand
92
93
**Usage Examples:**
94
95
```javascript
96
const reject = require('lodash.reject');
97
98
// Function predicate - reject active users
99
const users = [
100
{ name: 'Alice', active: true },
101
{ name: 'Bob', active: false },
102
{ name: 'Charlie', active: true }
103
];
104
105
const inactiveUsers = reject(users, user => user.active);
106
// => [{ name: 'Bob', active: false }]
107
108
// Object predicate - reject exact matches
109
const products = [
110
{ category: 'electronics', price: 100 },
111
{ category: 'books', price: 20 },
112
{ category: 'electronics', price: 200 }
113
];
114
115
const nonElectronics = reject(products, { category: 'electronics' });
116
// => [{ category: 'books', price: 20 }]
117
118
// Array predicate - reject by property-value pair
119
const items = [
120
{ type: 'urgent', priority: 1 },
121
{ type: 'normal', priority: 2 },
122
{ type: 'low', priority: 3 }
123
];
124
125
const nonUrgent = reject(items, ['type', 'urgent']);
126
// => [{ type: 'normal', priority: 2 }, { type: 'low', priority: 3 }]
127
128
// String predicate - reject by truthy property
129
const tasks = [
130
{ title: 'Task 1', completed: true },
131
{ title: 'Task 2', completed: false },
132
{ title: 'Task 3', completed: true }
133
];
134
135
const incompleteTasks = reject(tasks, 'completed');
136
// => [{ title: 'Task 2', completed: false }]
137
138
// With arrays of primitives
139
const numbers = [1, 2, 3, 4, 5, 6];
140
const evenNumbers = reject(numbers, n => n % 2 === 0);
141
// => [1, 3, 5]
142
143
// With objects (iterates over object values)
144
const scores = { alice: 85, bob: 92, charlie: 78 };
145
const belowNinety = reject(scores, score => score >= 90);
146
// => [85, 78]
147
```
148
149
## Implementation Notes
150
151
- Works with both arrays and objects (objects are iterated by their values)
152
- Uses internal `arrayFilter` for arrays and `baseFilter` for objects
153
- Predicate is normalized using `getIteratee(predicate, 3)` to handle different predicate types
154
- Returns a new array; does not modify the original collection
155
- For objects, the returned array contains the property values that don't match the predicate
156
- Supports the same iteratee shorthands as other lodash methods (`_.matches`, `_.matchesProperty`, `_.property`)