0

I'm currently receiving this error with the following code:

TypeError: data.data.trek.days[current_day] is undefined

The problem I am having is that I need to add the current_day along with an empty places array to the existing array structure if one hasn't been previously provided. How can I do 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
});
7
  • You're trying to see if data.data.trek.days[current_day].places has length? Commented May 21, 2013 at 19:30
  • UPDATE: When I try to do: data.data.trek.days.push(current_day); I get this error: TypeError: data.data.trek.days.push is not a function Commented May 21, 2013 at 19:30
  • 2
    is days even defined? print out via console.log(data.data.trek) and see if days exist as a member Commented May 21, 2013 at 19:31
  • No, I want to add the current_day as a key in the days array and add an empty places array as a value of that key. Commented May 21, 2013 at 19:31
  • Yes, days is defined and returns: 1 Object { places=[3]} 2 Object { places=[4]} Commented May 21, 2013 at 19:32

1 Answer 1

1

From your comment you seem to 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({ ... });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.