At least response.bookings can be either an object or an array.
if you what to find you what is you case just run console.log(response.bookings); and check the debugger console.
As an object it could look like (i added some fictional fields):
var response = {
status: 'ok',
bookings: {
//index: booking
a: {seat_id: 1, user_id: 4, paid: true},
b: {seat_id: 2, user_id: 3, paid: false},
c: {seat_id: 3, user_id: 2, paid: true},
d: {seat_id: 4, user_id: 1, paid: false}
}
};
//$.each means that function is called of each entry of response.bookings
$.each(response.bookings, function(index, booking) {
// frist run index='a', booking={seat_id: 1, user_id: 1, paid: true} so booking.seat_id=1
//second run index='b', booking={seat_id: 2, user_id: 2, paid: false} so booking.seat_id=2
// third run index='c', booking={seat_id: 3, user_id: 3, paid: true} so booking.seat_id=3
// fours run index='d', booking={seat_id: 4, user_id: 4, paid: false} so booking.seat_id=4
sc.status(booking.seat_id, 'unavailable');
// you can check the values by using console again:
// console.log(index);
// console.log(booking);
});
As an array it could look like (i added some fictional fields):
var response = {
status: 'ok',
bookings: [
// array of booking object (index incrementel starting from 0)
{seat_id: 1, user_id: 4, paid: true},
{seat_id: 2, user_id: 3, paid: false},
{seat_id: 3, user_id: 2, paid: true},
{seat_id: 4, user_id: 1, paid: false}
]
};
//$.each means that function is called of each entry of response.bookings
$.each(response.bookings, function(index, booking) {
// frist run index=0, booking={seat_id: 1, user_id: 1, paid: true} so booking.seat_id=1
//second run index=1, booking={seat_id: 2, user_id: 2, paid: false} so booking.seat_id=2
// third run index=2, booking={seat_id: 3, user_id: 3, paid: true} so booking.seat_id=3
// fours run index=3, booking={seat_id: 4, user_id: 4, paid: false} so booking.seat_id=4
sc.status(booking.seat_id, 'unavailable');
// you can check the values by using console again:
// console.log(index);
// console.log(booking);
});
in general is $.each only a jquery version of an for loop.
Here how you can do it using plain javascript (maybe helps you to unterstand ):
// response.bookings is an object
var booking;
for (index in response.bookings){
booking = response.bookings[index];
sc.status(booking.seat_id, 'unavailable');
//console.log(booking);
}
// response.bookings is an array
var booking;
for (var index = 0; index < response.bookings,length; index++){
booking = response.bookings[index];
sc.status(booking.seat_id, 'unavailable');
//console.log(booking);
}
{your_domain}/bookings/get/100; If this url is responding valid JSON thesuccessfunction is called whereresponseis the JSON object;$.eachrun a function on each item inresponse.bookings;sc.statusis some custom function that is called withbooking.seat_id$.each(response.bookings, function(index, booking) { //find seat by id and set its status to unavailable sc.status(booking.seat_id, 'unavailable');would you kindly show me how the JSON object would look like?. I will appreciate your responce