@Description: This emulates Array.prototype.reduce from javascript 1.6 and is taken from the MDC site reference. .reduce executes the callback function once for each element present in the array, excluding holes in the array, receiving four arguments: the initial value (or value from the previous callback call), the value of the current element, the current index, and the array over which iteration is occurring.
The call to the reduce callback would look something like this:
@File: Array/prototype/reduce.js
@Author: Paul Visco
@Version: 1.1 11/19/07
@Param: function The function to apply the array elements to
@Example:myArray.reduce(function(previousValue, currentValue, index, array){
// ...
});
//real examples
var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; });
// total == 6
var flattened = [[0,1], [2,3], [4,5]].reduce(function(a,b) {
return a.concat(b);
}, []);
// flattened is [0, 1, 2, 3, 4, 5]