The Wayback Machine - https://web.archive.org/web/20201220220817/https://github.com/lodash/lodash/blob/master/filter.js
Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
39 lines (37 sloc) 1.04 KB
/**
* Iterates over elements of `array`, returning an array of all elements
* `predicate` returns truthy for. The predicate is invoked with three
* arguments: (value, index, array).
*
* **Note:** Unlike `remove`, this method returns a new array.
*
* @since 5.0.0
* @category Array
* @param {Array} array The array 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 users = [
* { 'user': 'barney', 'active': true },
* { 'user': 'fred', 'active': false }
* ]
*
* filter(users, ({ active }) => active)
* // => objects for ['barney']
*/
function filter(array, predicate) {
let index = -1
let resIndex = 0
const length = array == null ? 0 : array.length
const result = []
while (++index < length) {
const value = array[index]
if (predicate(value, index, array)) {
result[resIndex++] = value
}
}
return result
}
export default filter
You can’t perform that action at this time.