Skip to content

Commit 3c02a9a

Browse files
author
David Brady
committed
Added EC5 version to arrays/filtering-arrays
1 parent cbbd45e commit 3c02a9a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

chapters/arrays/filtering-arrays.textile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ You want to be able to filter arrays based on a boolean condition.
1010

1111
h2. Solution
1212

13-
Extend the Array prototype to add a filter function which will take a callback and perform a comprension over itself, collecting all elements for which the callback is true.
13+
Use Array.filter (ECMAScript 5):
14+
15+
{% highlight coffeescript %}
16+
array = [1..10]
17+
18+
array.filter (x) -> x > 5
19+
# => [6,7,8,9,10]
20+
{% endhighlight %}
21+
22+
In pre-EC5 implementations, extend the Array prototype to add a filter function which will take a callback and perform a comprension over itself, collecting all elements for which the callback is true.
1423

1524
{% highlight coffeescript %}
1625
# Extending Array's prototype

0 commit comments

Comments
 (0)