Reputation: 1903
After an ajax request that's return an object I perform an $.each
'Cause I need to read all the object (appointment) returned. But seems that in the $.each the object is undefined, and I don't know why. This is my code of each request:
$.each(response.appointments, function(index, appointment)
{
var event =
{
'id': appointment['id'],
'title': appointment['service']['name'] + ' - '
+ appointment['customer']['first_name'] + ' '
+ appointment['customer']['last_name'],
'start': appointment['start_datetime'],
'end': appointment['end_datetime'],
'allDay': false,
'color': '#' + appointment['res_id']['hex_color'],
'data': appointment
};
calendarEvents.push(event);
});
this is the content of response
object returned in console.log(response):
And the appointment structure:
Someone have an idea to fix this?
Upvotes: 0
Views: 50
Reputation: 36458
If I'm reading your picture-of-an-object correctly (please, include actual text next time), response
is an array of objects, each containing appointments. In that case, you'll want to say:
$.each(response, function(index, rsp) {
$.each(rsp.appointments, function(index, appointment)
{
var event =
{
'id': appointment['id'],
'title': appointment['service']['name'] + ' - '
+ appointment['customer']['first_name'] + ' '
+ appointment['customer']['last_name'],
'start': appointment['start_datetime'],
'end': appointment['end_datetime'],
'allDay': false,
'color': '#' + appointment['res_id']['hex_color'],
'data': appointment
};
calendarEvents.push(event);
})
});
Upvotes: 2
Reputation: 21037
I think you referencing your object wrongly - try this:
$.each(response, function(index, item) {
$.each(item.apointments, function(idx2, appointment) {
....
Upvotes: 1