Search code snippets, questions, articles...

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

Search Index Data (The code snippet can also be found with below search text)

_.dropRightWhile lodash
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet