I have an array of objects where I wanna only save a sub object when it pass a condition. At the moment I first use map and then filter with the result:
var a = _.filter(
_.map(activities.toJSON(), function (activity) {
if (activity.verb !== 'test') {
return activity.object;
}
}), function(b){return b;});
This seems a bit odd. Is there a better way to do this.
Any time you start doing things like
_.f(_.g(...)), you should think about using_.chain. You could do something like this:I’ve also adjusted your logic to simplify things, there’s no need to do the same test twice. You can make this sort of thing quite readable if you put your functions into variables:
Demo: http://jsfiddle.net/ambiguous/EQfuH/
There will be a fair bit of copying with this approach so you might want to just go old-school and use a loop:
There’s nothing at all wrong with loops in JavaScript.