JavaScript tips — Remove undefined values from an array

Publicado em: 06 Outubro 2022
no canal de: Code 2020
2,597
14

The built-in filter method on #javascript arrays makes it easy to remove undefined values.

To only removed undefined values, use:

myArray.filter(function(x) { return x !== undefined; });

This will return a new array with all undefined values from 'myArray' removed. This does not modify the original array.

If you search online, you may see other resources suggesting doing this using:

myArray.filter(function(x) { return x; });

or

myArray.filter(Boolean);

These approaches may not be what you want as they will remove every falsy value from the array (such as 0, empty strings, and false) along with the undefined values

https://developer.mozilla.org/en-US/d...


Nesta página do site você pode assistir ao vídeo on-line JavaScript tips — Remove undefined values from an array duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Code 2020 06 Outubro 2022, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 2,597 vezes e gostou 14 espectadores. Boa visualização!