younes0
younes0

Reputation: 2302

delete current object in underscore.js each() or other

Delete current object in underscore.js each() or other ?

_.each(fools, function(user) {
    if (user['great'] === true) {  
        // delete user from fools object ?
    }
});

Upvotes: 3

Views: 4465

Answers (3)

younes0
younes0

Reputation: 2302

in each()

_.each(fools, function(user, key) {
    if (user['great'] === true) {  
        delete fools[key];
    }
});

Upvotes: 3

NT3RP
NT3RP

Reputation: 15370

Have you considered _.reject?

From the underscore documentation:

Returns the values in list without the elements that the truth test (iterator) passes. The opposite of filter.

var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=> [1, 3, 5]

Upvotes: 2

Prinzhorn
Prinzhorn

Reputation: 22508

You probably want to use filter http://underscorejs.org/#filter

fools = _.filter(fools, function(user) {
    return !user['great'];
});

Or reject, which is just sugar.

Upvotes: 5

Related Questions