Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    }
});
like image 494
Angular Avatar asked Jun 27 '16 19:06

Angular


People also ask

Can a map return an empty array?

map() calls a function once for each element in an array. map() does not execute the function for empty elements.

Can a map return null?

A map key can hold the null value. Adding a map entry with a key that matches an existing key in the map overwrites the existing entry with that key with the new entry. Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries.

How do I remove an item from an array in maps?

Map.delete() Method in JavaScript The Map. delete() method in JavaScript is used to delete the specified element among all the elements which are present in the map.


1 Answers

Use Array.prototype.filter:

Copy.names = rule.names.filter(function(x) {                                
    return x.name !=='Everything';
}).map(function (x) {
    return x.name;
});
like image 149
Dylon Avatar answered Sep 20 '22 19:09

Dylon