0
# Lodash Reject
1
2
Lodash Reject is a standalone module that exports the Lodash `_.reject` method for filtering collections by removing elements that pass a predicate test. It provides the opposite functionality of filter - returning elements that do NOT match the specified 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
var reject = require('lodash.reject');
15
```
16
17
For ES6 modules:
18
19
```javascript
20
import reject from 'lodash.reject';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var reject = require('lodash.reject');
27
28
var users = [
29
{ 'user': 'barney', 'age': 36, 'active': false },
30
{ 'user': 'fred', 'age': 40, 'active': true }
31
];
32
33
// Remove inactive users
34
var activeUsers = reject(users, function(o) { return !o.active; });
35
// => [{ 'user': 'fred', 'age': 40, 'active': true }]
36
37
// Using property shorthand - remove active users
38
var inactiveUsers = reject(users, 'active');
39
// => [{ 'user': 'barney', 'age': 36, 'active': false }]
40
```
41
42
## Capabilities
43
44
### Collection Filtering
45
46
Filters collection elements that do NOT pass the predicate test, returning a new array with elements that failed the test.
47
48
```javascript { .api }
49
/**
50
* The opposite of _.filter; this method returns the elements of collection
51
* that predicate does NOT return truthy for.
52
* @param {Array|Object} collection - The collection to iterate over
53
* @param {Function|Object|Array|string} [predicate=_.identity] - The function invoked per iteration
54
* @returns {Array} Returns the new filtered array
55
*/
56
function reject(collection, predicate);
57
```
58
59
**Predicate Types:**
60
61
- **Function**: `function(value, index|key, collection) { return boolean; }`
62
- **Object**: `{ key: value }` - matches objects with specified properties
63
- **Array**: `['property', value]` - matches objects where property equals value
64
- **String**: `'property'` - checks property truthiness
65
66
**Usage Examples:**
67
68
```javascript
69
var users = [
70
{ 'user': 'barney', 'age': 36, 'active': false },
71
{ 'user': 'fred', 'age': 40, 'active': true },
72
{ 'user': 'pebbles', 'age': 1, 'active': false }
73
];
74
75
// Function predicate - reject inactive users
76
reject(users, function(o) { return !o.active; });
77
// => [{ 'user': 'fred', 'age': 40, 'active': true }]
78
79
// Object matching - reject users matching criteria
80
reject(users, { 'age': 40, 'active': true });
81
// => [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
82
83
// Property-value pair - reject users with specific property value
84
reject(users, ['active', false]);
85
// => [{ 'user': 'fred', 'age': 40, 'active': true }]
86
87
// Property name - reject users where property is truthy
88
reject(users, 'active');
89
// => [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
90
91
// With arrays of primitive values
92
reject([1, 2, 3, 4, 5], function(n) { return n % 2 === 0; });
93
// => [1, 3, 5]
94
95
// With object collections
96
var inventory = {
97
'apple': { count: 5, expired: false },
98
'banana': { count: 0, expired: true },
99
'orange': { count: 3, expired: false }
100
};
101
102
reject(inventory, function(item) { return item.expired || item.count === 0; });
103
// => { 'apple': { count: 5, expired: false }, 'orange': { count: 3, expired: false } }
104
```