Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

reduce

The reduce() method of the Array object is used to reduce an array to a single value or to flatten a multidimensional array.

Syntaxarray.reduce( callback, initialValue )

Parameters

callback Reference a callback function that will reduce the array. previous - represents the previous value in the accumulative reduction. current - represents the current element of the array as they pass through the callback. index - indicates the index number of the element passing through the callback. array - a reference to the array object calling the every() method. initialValue ( optional ) Prepend an initial value to the reduction that is not part of the array.

Examples

Total the values in a numeric array. function callback(p, c, i, a){ return p + c; } var arr = [4, 2, 1]; alert( arr.reduce(callback) );

JavaScript

Methods

addEventListeneralertaveragebackblurconcatcreatedecodeURIdecodeURIComponentencodeURIencodeURIComponentevaleveryfilterforwardgoisArrayisFiniteisNaNjoinparseFloatparseIntpoppushpushStatereducereplaceStatereverseshiftshuffleslicesomesortspliceunshiftwrite