Filter does just that, it filters. Discover how it takes an array containing items of a given type. It loops through them and based on the closure returning true, it keeps the item. The rest it removes. The result is a collection of the same type with the same number or less items. It will be less all the items filtered out.
- [Instructor] So the next higher order function…we're going to look at is filter.…So filter does what it says.…It filters.…It filters out items from a collection.…So once again, just like the other ones we've looked at,…it takes a closure with one parameter.…Just like we saw before with map, and the parameter type…is the type of the items in our array.…And it returns a bool.…If your closure returns true, it keeps it.…If it returns false, it filters it out.…So the result of filter is going to be an array…of the same type as your collection.…
So strings, ints, whatever the original one,…as the same size or smaller.…If you don't filter out any,…it's just going to be another array of the same things.…So let's take a look at that in our playground.…Okay so we have this array of names,…and we want to filter out any names with a length…less than five.…So we know we're going to filter out these first two.…I'll put another one at the end that's short.…I'll just say it's ASD,…so that we know that we can filter out some…from the beginning, and any other position in the array.…
- What are closures?
- Closures and function types
- Basic higher-order functions, including sort and forEach
- Powerful higher-order functions, including map and reduce
- Passing functions
- Closure optimizations
- Higher-order functions on sets, dictionaries, and strings