Open In App

Lodash _.rangeRight() Method

Lodash _.rangeRight() method is used to create an array of numbers progressing from the given start value to the given, but not including, the given end value. It populates the values in descending order. A step value of -1 is used if a negative start is specified without an end or step. If the end is not specified, it’s set to start with the start then set to 0.

Syntax:

_.rangeRight( start, end, step )

Parameters:

This method accepts three parameters as mentioned above and described below:



Return Value:

It returns an array with the range of numbers in descending order.

Example 1: In this example, the code utilizes the Lodash library’s _.rangeRight method to generate an array of numbers in reverse order and displays the result in the console.






// Requiring the lodash library 
const _ = require("lodash");           
   
// Using the _.rangeRight() method
let range_arr = _.rangeRight(6);
       
// Printing the output 
console.log(range_arr);

Output:

[5, 4, 3, 2, 1, 0]

Example 2: In this example, the code uses Lodash’s _.rangeRight method to create an array of numbers in reverse order, starting from 15 and decreasing by 3, and then it prints the resulting array in the console.




// Requiring the lodash library 
const _ = require("lodash");           
   
// Using the _.rangeRight() method
// with the step taken as 3
let range_arr = _.rangeRight(0,15,3);
       
// Printing the output 
console.log(range_arr);

Output:

[12, 9, 6, 3, 0]

Example 3: In this example, the code requires the Lodash library and utilizes the _.rangeRight method with a step of -2 to generate an array of numbers in reverse order, starting from 0 and decreasing by 2, and then displays the result in the console.




// Requiring the lodash library 
const _ = require("lodash");           
   
// Using the _.rangeRight() method
// with the step taken as -2
let range_arr = _.rangeRight(0,-10,-2);
       
// Printing the output 
console.log(range_arr);

Output:

[-8, -6, -4, -2, 0]

Article Tags :