How to use the reduceRight function from lodash

Find comprehensive JavaScript lodash.reduceRight code examples handpicked from public code repositorys.

lodash.reduceRight is a JavaScript utility function that iterates over an array or object from right to left and applies a callback function to each element, returning a single accumulated value.

4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
 * @param {*} [thisArg] The `this` binding of `callback`.
 * @returns {*} Returns the accumulated value.
 * @example
 *
 * var list = [[0, 1], [2, 3], [4, 5]];
 * var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
 * // => [4, 5, 2, 3, 0, 1]
 */
function reduceRight(collection, callback, accumulator, thisArg) {
  var noaccum = arguments.length < 3;
fork icon73
star icon711
watch icon29

316
317
318
319
320
321
322
323
324
325
module.exports.rangeRight          = _.rangeRight;
module.exports.rcurry2             = _.rcurry2;
module.exports.rcurry3             = _.rcurry3;
module.exports.rearg               = _.rearg;
module.exports.reduce              = _.reduce;
module.exports.reduceRight         = _.reduceRight;
module.exports.reductions          = _.reductions;
module.exports.reject              = _.reject;
module.exports.remove              = _.remove;
module.exports.renameKeys          = _.renameKeys;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.reduceRight work?

lodash.reduceRight is a higher-order function in JavaScript that applies a callback function to each element of an array, starting from the right end of the array to the left, accumulating the results in a single value. The callback function takes four arguments: the accumulator, the current element, the current index, and the original array.

200
201
202
203
204
205
206
207
208
209
  const lodashCallback = (a, b) => a + b
  const ramdaCallback = (a, b) => b + a
  const nativeCallback = (a, b) => a + b
  return {
    iiris: () => A.reduceRight(iirisCallback, 0, array),
    lodash: () => _.reduceRight(array, lodashCallback, 0),
    ramda: () => R.reduceRight(ramdaCallback, 0, array),
    native: () => array.reduceRight(nativeCallback, 0),
  }
},
fork icon1
star icon31
watch icon0

257
258
259
260
261
262
263
264
265
266
267
268
269
console.log(partition); // => [[1, 3], [2]]


const reduce = _.reduce([1, 2], (sum, n) => sum + n, 0);
console.log(reduce); // => 3


const reduceRight = _.reduceRight([1, 2], (sum, n) => sum + n, 0);
console.log(reduceRight); // => 3


const reject = _.reject([4, 5, 6], n => n % 2 == 0);
console.log(reject); // => [5]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const array = [1, 2, 3, 4, 5];

const sum = _.reduceRight(
  array,
  (accumulator, currentValue) => {
    return accumulator + currentValue;
  },
  0
);

console.log(sum); // Output: 15

In this example, lodash.reduceRight iterates over the array from right to left and accumulates the value of all the elements in the array using the provided reducer function. The initial value of the accumulator is 0, so the sum of all the elements in the array is returned.

155
156
157
158
159
160
161
162
163
164
165
166
// log(result);


// reduce right


let arr = ['a', 'b', 'c', 'd', 'e'];
// result = _.reduceRight(arr, (prev,ele) => prev.concat(ele.toUpperCase()), '');
// log(result);
// result = _.reduce(arr, (prev,ele) => prev.concat(ele.toUpperCase()), '');
// log(result);

fork icon0
star icon0
watch icon0

25
26
27
28
29
30
31
32
33
34
35
36
37
// let sum = _.reduce(nums, (total, next) => { return total + next });
// console.log(sum);


// let colours = ["red", "green", "white", "blue", "black"];


// let res = _.reduceRight(colours, (next, total) => { return `${total}-${next}` });
// console.log(res);
// In the example, we use the reduce operation on a list of integers and strings.


// let sum = _.reduce(nums, (total, next) => { return total + next });
fork icon0
star icon0
watch icon0

+ 2 other calls in file

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)