The following functions are related in:
The function dojo.filter
takes two arguments: an array and a filtering function and returns an array. Suppose that you want to do the following:
function makeNewArray(arg_arr, ceiling) { var new_arr = []; for(var i = 0; i < arg_arr.length; i++) { var element = arg_arr[i]; if (element > ceiling) { new_arr.push(element); } } return new_arr; }
The same code can be expressed like this using the filter function:
function makeNewArray(arg_arr, ceiling) { return dojo.filter(arg_arr, function(x){ return x > ceiling; }) }
What happened now? Most importantly, all intermediate index variables and temporary variables evaporated, reducing the risk for errors drastically.
In some more detail, dojo.filter
applies the function argument on every element of the array. If the function returns true, dojo.filter
will add the element to an invisible return value array. If it returns numerical zero it does not.
It will also coerce other...