I am trying to use underscore.js _.map
function on an array of objects, to get an array with a property of each object. That's the usual scenario, so :
var finalArray = _.map(myArray, function(obj) {
return obj.myProperty;
});
But in some cases I need that nothing be added in the array. It could be something like :
var finalArray = _.map(myArray, function(obj) {
if (!obj.ignore) {
return obj.myProperty;
}
});
The result of this is that an undefined
value is pushed into the array, which is not the same as not pushing anything at all.
Is there a way for the map function not to push a value, or do I need to post-process my finalArray
to remove the unwanted undefined
's?
you should use _.filter() before _.map()