Use the wonderful underscore lib to remove the undefined elements