how to return nothing ( empty array) with array.map function

Angular picture Angular · Jun 27, 2016 · Viewed 28.1k times · Source

Right now, if 'Everything' in the list is detected, the output becomes [""].
Expected output: []

Copy.names = rule.names.map(function(x) {                                
    if (x.name ==='Everything') {                                   
        return '';
    } else {
        return x.name;
    }
});

Answer

Dylon picture Dylon · Jun 27, 2016

Use Array.prototype.filter:

Copy.names = rule.names.filter(function(x) {                                
    return x.name !=='Everything';
}).map(function (x) {
    return x.name;
});