Testing Every Element
You want to be able to check that every element in an array meets a particular condition.
Use Array.every (ECMAScript 5):
evens = (x for x in [0..10] by 2) evens.every (x)-> x % 2 == 0 # => true
_.all from underscore.js.
For a real world example, prentend you have a multiple select list that looks like:
<select multiple id="my-select-list"> <option>1</option> <option>2</option> <option>Red Car</option> <option>Blue Car</option> </select>
Now you want to verify that the user selected only numbers. Let's use Array.every:
validateNumeric = (item)-> parseFloat(item) == parseInt(item) && !isNaN(item) values = $("#my-select-list").val() values.every validateNumeric
This is similar to using ruby's Array#all? method.