The _.foldr()
function, also known as reduceRight()
, is part of the Underscore library, which is a functional programming library for JavaScript. _.foldr()
applies a function against an accumulator and each value of an array from right to left, reducing it to a single value.
Syntax:
index.tsx49 chars2 lines
Parameters:
array
: The input array to iterate over.iteratee
: The function to apply to each element in the array.accumulator
: Used to keep track of the computation progress.context
(optional): this
keyword context for iteratee
function.Example usage:
index.tsx238 chars10 lines
In this example, _.foldr()
applies the function of adding two numbers -- the iteratee
function -- to each element of the array starting from the right side of the array. The accumulator
starts with a value of 0, to which the result of each computation is added. At the end, the final result is the sum of all the numbers in the array, which is logged to the console.
gistlibby LogSnag