Last Updated : 03 Sep, 2024
Lodash _.reduceRight() method is similar to the _.reduce() method except that it iterates over elements of collection from right to left.
Syntax:_.reduceRight(collection, iteratee, accumulator)Parameters:
This method accepts three parameters as mentioned above and described below:
This method returns the accumulated value.
Example 1: This code uses the Lodash library to flatten a nested array by iterating from right to left and concatenating subarrays using the _.reduceRight()
method.
Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array = [[10, 11], [12, 13], [14, 15]];
// Use of _.reduceRight() method
let gfg = _.reduceRight(array,
function(flattened, other) {
return flattened.concat(other);
}, []);
// Printing the output
console.log(gfg);
Output:
[ 14, 15, 12, 13, 10, 11 ]
Example 2: In this example, the code requires the Lodash library and uses the _.reduceRight
method to flatten a nested array and then displays the flattened result in the console.
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array = [['C++', 'C#'],
['DAA', 'Java'], ['Lodash', 'Python']];
// Use of _.reduceRight() method
let gfg = _.reduceRight(array,
function(flattened, other) {
return flattened.concat(other);
}, []);
// Printing the output
console.log(gfg);
Output:
[ 'Lodash', 'Python', 'DAA', 'Java', 'C++', 'C#' ]
Note:
This code will not work in normal JavaScript because it requires the library lodash to be installed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4