forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filterObject.js
34 lines (32 loc) · 946 Bytes
/
filterObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Iterates over properties of `object`, returning an array of all elements
* `predicate` returns truthy for. The predicate is invoked with three
* arguments: (value, key, object).
*
* If you want an object in return, consider `pickBy`.
*
* @since 5.0.0
* @category Object
* @param {Object} object The object to iterate over.
* @param {Function} predicate The function invoked per iteration.
* @returns {Array} Returns the new filtered array.
* @see pickBy, pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, reject
* @example
*
* const object = { 'a': 5, 'b': 8, 'c': 10 }
*
* filterObject(object, (n) => !(n % 5))
* // => [5, 10]
*/
function filterObject(object, predicate) {
object = Object(object)
const result = []
Object.keys(object).forEach((key) => {
const value = object[key]
if (predicate(value, key, object)) {
result.push(value)
}
})
return result
}
export default filterObject