Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptLodash _.reduceRight() Method

Lodash _.reduceRight() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers, etc.
The _.reduceRight() method is similar to _.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:

  • collection: This parameter holds the collection to iterate over.
  • iteratee: This parameter holds the function invoked per iteration.
  • accumulator: This parameter holds the initial value.

Return Value: This method returns the accumulated value.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var 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:




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var 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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments