You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You want to be able to check that every element in an array meets a particular condition.
9
+
10
+
## Solution
11
+
12
+
Use Array.every (ECMAScript 5):
13
+
14
+
{% highlight coffeescript %}
15
+
evens = (x for x in [1..10] by 2)
16
+
17
+
evens.every (x)-> x % 2 == 0
18
+
# => true
19
+
{% endhighlight %}
20
+
21
+
Array.every was addded to Mozilla's Javascript 1.6 and made standard with EcmaScript 5. If you to support browsers that do not implement EC5 then check out [`_.all` from underscore.js][underscore].
22
+
23
+
For a real world example, prentend you have a multiple select list that looks like:
24
+
25
+
{% highlight html %}
26
+
<selectmultipleid="my-select-list">
27
+
<option>1</option>
28
+
<option>2</option>
29
+
<option>Red Car</option>
30
+
<option>Blue Car</option>
31
+
</select>
32
+
{% endhighlight %}
33
+
34
+
Now you want to verify that the user selected only numbers. Let's use Array.every:
0 commit comments