check for undefined  before calling $.each