Lodash _.takeRightWhile() Method
Last Updated :
03 Nov, 2023
Lodash _.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 the predicate returns falsely.
Syntax:
_.takeRightWhile(array, [predicate=_.identity]);
Parameters:
- 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 the array.
Example 1: In this example, we are getting an array of objects that satisfy the given condition in a function.
javascript
const _ = require( "lodash" );
let users = [{
'user' : 'fred' , 'active' : false
},
{ 'user' : 'pebbles' , 'active' : false }];
let ind = _.takeRightWhile(users, function (o) {
return !o.active;
});
console.log(ind);
|
Output:
[{ user: 'fred', active: false }, {user: 'pebbles', active: false}]
Example 2: In this example, we are getting an array of objects that satisfy the given condition in a function.
javascript
const _ = require( "lodash" );
let users = [{
'user' : 'fred' , 'active' : false
},
{ 'user' : 'pebbles' , 'active' : false }];
let gfg = _.takeRightWhile(users, {
'user' :
'pebbles' , 'active' : false
});
console.log(gfg);
|
Output:
[{user: 'pebbles', active: false}]
Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.
Share your thoughts in the comments
Please Login to comment...