javascript

Remove items starting from last untill given condition is false Lodash

To remove array items starting from last until the given condition is false, you can use dropRightWhile method of lodash javascript library

var users = [
    { 'user': 'Ankit', 'active': true },
    { 'user': 'John', 'active': false },
    { 'user': 'Deo', 'active': false }
];

const result = _.dropRightWhile(users, function (o) { 
    return !o.active;
});

// { 'user': 'Ankit', 'active': true }
Output
{ 'user': 'Ankit', 'active': true }

_.dropRightWhile Lodash

It cretes a slice of array which excludes some elements from the end. Elements are removed or deleted from the end until the predicate return falsey values.

Example Demo

Was this helpful?