Skip to content
Related Articles

Related Articles

Lodash _.takeRightWhile() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Aug, 2020

The _.takeRightWhile the method is used to create a slice of an array in which elements are taken from the end and these elements are taken until predicate returns falsely.

Syntax:

_.takeRightWhile(array, [predicate=_.identity])

Parameters: This method accepts two parameters as mentioned above and described below:

  • array: This parameter holds the array to query.
  • [predicate=_.identity]: This parameter holds the function invoked per iteration.

Return Value: This method is used to return the slice of array.

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

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = [{ 'user': 'fred',    'active': false 
  
},
  { 'user': 'pebbles', 'active': false }];
      
  
// Use of _.takeRightWhile() 
// method 
let ind = _.takeRightWhile(users, function(o) { 
  
return !o.active; }); 
      
// Printing the output 
console.log(ind);


Output:

[{ user: 'fred', active: false }, {user: 'pebbles', active: false}]

Example 2:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = [{ 'user': 'fred',    'active': false 
  
},
  { 'user': 'pebbles', 'active': false }];
      
// Use of _.takeRightWhile() 
// method 
// The `_.matches` iteratee shorthand.
let gfg = _.takeRightWhile(users, { 'user'
  
'pebbles', 'active': false }); 
      
// Printing the output 
console.log(gfg);


Output:

[{user: 'pebbles', active: false}]

Example 3:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = [{ 'user': 'fred',    'active': false 
  
},
  { 'user': 'pebbles', 'active': false }];
      
  
// Use of _.takeRightWhile() 
// method
// The `_.matchesProperty` iteratee shorthand.
   
let gfg = _.takeRightWhile(users, ['active'
  
false]); 
      
// Printing the output 
console.log(gfg);


Output:

[{ user: 'fred', active: false }, {user: 'pebbles', active: false}]

Example 4:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = [{ 'user': 'fred',    'active': false 
  
},
  { 'user': 'pebbles', 'active': false }];
      
  
// Use of _.takeRightWhile() 
// method
 // The `_.property` iteratee shorthand.
   
let gfg =_.takeRightWhile(users, 'active');
      
// Printing the output 
console.log(gfg);


Output:

[]

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!