Files
lodash/filterObject.js
John-David Dalton aa5e1b2fe0 Fix object coercion.
2017-04-16 15:30:42 -05:00

33 lines
880 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) {
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