-
Notifications
You must be signed in to change notification settings - Fork 1
/
filterObject.js
26 lines (22 loc) 路 823 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
/**
* Iterates over properties of an object `obj` returning an array of all
* elements `fn` (predicate) returns truthy for.
*
* Note that the predicate is invoked with three arguments: `value`, `key`, `object`.
*
* @param {Object} obj The object to iterate over.
* @param {Function} fn The predicate function invoked per iteration.
* @return {Array} Returns the new filtered array.
* @example
*
* const object = { 'a': 5, 'b': 8, 'c': 10 }
* filterObject((n) => !(n % 5), object) // [5, 10]
*
*/
// const filterObject = (obj, fn) => Object.keys(obj).filter(key => fn(obj[key], key, obj) ? obj[key] : null)
const filterObject = (fn, obj) => {
const result = []
Object.keys(obj).forEach(key => fn(obj[key], key, obj) ? result.push(obj[key]) : null)
return result
}
export default filterObject