JavaScript tips — Remove undefined values from an array

preview_player
Показать описание
The built-in filter method on #javascript arrays makes it easy to remove undefined values.

To only removed undefined values, use:

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:

or

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

Рекомендации по теме
Комментарии
Автор

Just found this hidden gem of a playlist. Best tutorial I've come across

grathiumsoftwares
Автор

It's really helpful. Keep creating things like this. !

baodoan