I have the following array:
var imagesList = [undefined x 1, cars.jpg, undefined x 1, boats.jpg];
How do I filter out the undefined? so I can get it as follows:
var imagesShow = [cars.jpg, boats.jpg];
I have not found much documentation on getting rid of undefined in an array using javascript.
You could use Array#filter
with Boolean
as callback for truthy values.
var imagesList = [undefined, 'cars.jpg', undefined, 'boats.jpg'],
imagesShow = imagesList.filter(Boolean);
console.log(imagesShow);
Use Array#filter
:
var imagesList = [undefined, 'cars.jpg', undefined, 'boats.jpg'];
var result = imagesList.filter(function(v) {
return v !== undefined;
})
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With