In today’s article, we will learn about how to remove array elements from the last until a specified condition is not satisfied in an array. To do so we can use _.dropRightWhile() function of lodash in JavaScript.
Table of Contents
About Lodash’s .dropRightWhile() Method
Similar to the Lodash’s dropWhile() function, this function is also used to return the value of the given array after slicing. dropRightWhile() method will remove the array elements from the last until the given condition is satisfied.
Syntax
_.dropRightWhile(array, [predicate=_.identity])
First parameter is array which needs to be sliced and the second argument will be the function that will either returns true or false depending on the condition given to make it stop slicing the array further.
Code
// Requiring the Lodash library
const _ = require("lodash");
var users = [
{ 'user': 'Arpit', 'active': true },
{ 'user': 'Vijay', 'active': false },
{ 'user': 'Ishika', 'active': false }
];
const result = _.dropRightWhile(users, function (o) {
return !o.active;
});
console.log(result);
Output
{ 'user': 'Arpit', 'active': true }