7

This is my stringified JSON object:

{ "bookingsList": [{ "bookingID": 5, "destination": "BSO", "flightDate": "2015-12-07T00:00:00", "flightScheduleId": 113, "origin": "MNL", "passengerList": [{ "availedInsurance": true, "availedMeal": false, "birthDate": null, "carryOnBaggage": 5.1, "checkInBaggage": 4.5, "columnId": 1, "firstname": "Peter", "gender": "M", "lastname": "North", "middlename": "West", "nationality": null, "rowId": "A" }] }, { "bookingID": 6, "destination": "BSO", "flightDate": "2015-12-07T00:00:00", "flightScheduleId": 113, "origin": "MNL", "passengerList": [{ "availedInsurance": false, "availedMeal": false, "birthDate": null, "carryOnBaggage": 4.2, "checkInBaggage": 3.4, "columnId": 2, "firstname": "Mark Justin", "gender": "M", "lastname": "Jalandoni", "middlename": "Manzano", "nationality": null, "rowId": "A" }] }] } 

So how do I Iterate over the objects in the bookingsList? I will be placing some properties on a table.

EDIT: turns out that for in loop I was using wasn't iterating because I forgot to place a "var" prior to the object variable in the iterable.

2
  • bookingsList is just an array so any looping construct will do, a plain old for loop for example. Commented Jan 26, 2016 at 14:26
  • do you know looping concept??? Commented Jan 26, 2016 at 14:32

2 Answers 2

2

A simple for loop should suffice:

var jsonObj = { "bookingsList": [{ "bookingID": 5, "destination": "BSO", "flightDate": "2015-12-07T00:00:00", "flightScheduleId": 113, "origin": "MNL", "passengerList": [{ "availedInsurance": true, "availedMeal": false, "birthDate": null, "carryOnBaggage": 5.1, "checkInBaggage": 4.5, "columnId": 1, "firstname": "Peter", "gender": "M", "lastname": "North", "middlename": "West", "nationality": null, "rowId": "A" }] }, { "bookingID": 6, "destination": "BSO", "flightDate": "2015-12-07T00:00:00", "flightScheduleId": 113, "origin": "MNL", "passengerList": [{ "availedInsurance": false, "availedMeal": false, "birthDate": null, "carryOnBaggage": 4.2, "checkInBaggage": 3.4, "columnId": 2, "firstname": "Mark Justin", "gender": "M", "lastname": "Jalandoni", "middlename": "Manzano", "nationality": null, "rowId": "A" }] }] } // For easy reference var bookingsList = jsonObj.bookingsList; for (i = 0; i < bookingsList.length; i++) { var booking = bookingsList[i]; // Grab booking data console.log(booking.bookingID); // Log booking data } 
Sign up to request clarification or add additional context in comments.

Comments

0
$.each(json.bookingsList, function(index, prop){ console.log(prop); }); 

1 Comment

Code-only answers aren't really useful in helping the OP understand what is happening. Maybe give a short explanation of your answer.