JavaScript tips — Remove undefined values from an array

Опубликовано: 06 Октябрь 2022
на канале: 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...


На этой странице сайта вы можете посмотреть видео онлайн JavaScript tips — Remove undefined values from an array длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Code 2020 06 Октябрь 2022, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 2,597 раз и оно понравилось 14 зрителям. Приятного просмотра!