Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove undefined from a list of items?

How to prevent appearing undefined in the output?

var item1 = $(result).find('.item:nth-child(2) a').html(),
    item2 = $(result).find('.item:nth-child(3) a').html(),
    item3 = $(result).find('.item:nth-child(4) a').html(),
    item4 = $(result).find('.item:nth-child(5) a').html();

$('div').html(item1+item2+item3+item4);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could put them all in an array and then filter it.

const array = [item1, item2, item3, item4];
const onlyDefined = array.filter(item => item !== undefined);

If you’re not doing anything else with the variables, you could also do it all in chunk, by looping over the numbers 2 through 5.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading