JavaScript tips — Remove undefined values from an array

Publicado el: 06 octubre 2022
en el 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...


En esta página del sitio puede ver el video en línea JavaScript tips — Remove undefined values from an array de Duración hora minuto segunda en buena calidad , que subió el usuario Code 2020 06 octubre 2022, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 2,597 veces y le gustó 14 a los espectadores. Disfruta viendo!