Skip to content

Latest commit

 

History

History
25 lines (16 loc) · 1.28 KB

2017-06-16-playing-with-arrays-part-II.md

File metadata and controls

25 lines (16 loc) · 1.28 KB
layout title subtitle bigimg
post
Playing with Arrays - part II
filter function
../img/analog.jpg

The filter function lets you quickly filter data from an array without using a forEach or for loop.

We'll use the same object as before.

{% highlight javascript %} var symbols = getHighPrices([ {symbol:"GG", price:500.65}, {symbol:"AA", price:1085.20}, {symbol:"BB", price:135.00} ]); {% endhighlight %}

The great thing about filter function is that you can quickly filter your array without using forEach or a standard for loop.

Let's have a look on how to return stocks with prices higher than 450.00, it's easy:

{% highlight javascript %} function getHighPrices(stocks){ return stocks.filter(function(stock){ return stock.price >= 400; }) }; {% endhighlight %}

The main part of the above is a predicate function (function(stock{...}), a function which returns True or False based on a condition, in this case the condition is 'stock.price >= 400'. If the result is True, the filter function pushes the 'current' stock element (e.g. {symbol:"GG", price:500.65}) to an array. This Array is then returned by using:

{% highlight javascript %} return stocks.filter(... {% endhighlight %}

This way we can quickly filter arrays. In the next post I'll show you how to chain map and filter!