mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
30 lines
837 B
JavaScript
30 lines
837 B
JavaScript
/**
|
|
* 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).
|
|
*
|
|
* @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 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) {
|
|
const result = []
|
|
Object.keys(Object(object)).forEach((key) => {
|
|
if (predicate(object[key], key, object)) {
|
|
result.push(value)
|
|
}
|
|
})
|
|
return result
|
|
}
|
|
|
|
export default filterObject
|