User Tools

Site Tools


java-script:reduce

Java Script Reduce function

reduce is a method that applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

This can be very useful for tasks such as summing all the values in an array or reducing an array of objects into a more useful structure.

Here's the general syntax:

array.reduce(callback[, initialValue])

Callback

This is the function to execute on each element in the array, taking four arguments:

  • accumulator: The accumulated value previously returned in the last invocation of the callback.
  • currentValue: The current element being processed in the array.
  • currentIndex: (Optional) The index of the current element being processed.
  • array: (Optional) The array on which reduce was called.

Initial Value

initialValue - (Optional) Value to use as the first argument to the first call of the callback.

java-script/reduce.txt · Last modified: 2023/08/07 20:47 by odefta