Task.map

jameswestgate edited this page Apr 27, 2012 · 2 revisions
Clone this wiki locally

map(array, function(index, value))

Updates each element of the array provide with the result of the function parameter. In the following example, the map function is used to fill an array with integers 1 to 9.

// -- Test Map / reduce --
var values = new Array(9);

//Add functions to place the values 1 to 9 in an array
var task = new Task();
task.map(values, function (index, value) {
    return index + 1;
});

//Add a function to sum the values from 0 to 9
task.reduce(values, 0, function (index, value, total) {
    return (total + value);
});

task.execute({async: false}, function (result) {
    ok(result == 45, 'Simple reduced result is ' + result);
});

See also reduce, execute.