javascript - Add an Empty Array to An Array with Existing Values? -


i'm receiving error following code:

typeerror: data.data.trek.days[current_day] undefined

the problem having need add current_day along empty places array existing array structure if 1 hasn't been provided. how can this?

data.data.trek.days[current_day].places.push({     "url": url,     "img_src": img_src,     "title": title,     "time_going_hour": time_going_hour,     "time_going_minutes": time_going_minutes,     "duration": "3600",     "id": id,     "type": type,     "city_id": city_id }); 

from comment seem want this:

data.data.trek.days = data.data.trek.days || {}; data.data.trek.days[current_day] = data.data.trek.days[current_day] || {}; data.data.trek.days[current_day].places = data.data.trek.days[current_day].places || []; data.data.trek.days[current_day].places.push({ ... }); 

Comments