Lindsey Kuper edited this page Feb 12, 2015 · 7 revisions


filter expects an elemental function as its sole argument. The elemental function takes an index as an argument, and the source ParallelArray is bound to this within its body; it is expected to return a truth value that indicates whether the source array's element at the given index position should be included in the result.


myParallelArray.filter(elementalFunction, arg1, arg2, ...)


  • elementalFunction: described below.
  • arg1, arg2, ...: passed unchanged to elementalFunction.

Elemental Function

function(index, arg1, arg2, ...) { <body> }
  • index: The location in this where the source element is found.
  • arg1, arg2, ...: The same as the optional arguments passed to filter.

Inside the elemental function, the value of this will be the ParallelArray object on which filter was invoked.

The elemental function is expected to return:

  • true (true, 1, or other JavaScript truthy value) if the source element should be placed in filter's result.
  • false (false, 0, undefined, or other JavaScript falsey value) if the source element should not be placed in filter's result.


A freshly minted ParallelArray containing the source elements for which the result of applying the elemental function is true. The order of the elements in the returned ParallelArray is the same as the order of the elements in the source ParallelArray.


// an identity function on a ParallelArray `pa`
pa.filter(function(){return true;})

// `even` is a ParallelArray containing only those elements of `source` that are even
var source = new ParallelArray([1,2,3,4,5]);
var even = source.filter(function even(iv) { return (this.get(iv) % 2) == 0; });