2

Via AJAX i'm getting a JSON object, I want to look though all these items.. here is my ajax

        document.getElementById('ACOptions').innerHTML = "";
    $.ajax({url: "SearchAvailableAircraft.php?ID=<?php echo $SimID; ?>&Code=<?php echo $SimCode; ?>&Search=" + S, success: function(result){
                /*alert(result);*/
                var MyAircraftList = result.AircraftList;

And below is my response

{"status":200,"status_message":"Valid Account","AircraftList":[{"ID":"1","FullTXT":"Boeing 777-200ER","TypeCode":"772","Manufacturer":"Boeing","Model":"777","Variant":"200ER","PaxCnt":"305","RangeNM":"5240","MinRwFT":"8000","Cost":"261500000","DeliveryDelay":"18"},{"ID":"2","FullTXT":"Airbus A320-200","TypeCode":"320","Manufacturer":"Airbus","Model":"A320","Variant":"200","PaxCnt":"186","RangeNM":"3300","MinRwFT":"2100","Cost":"98000000","DeliveryDelay":"9"}]}

How can I loop though these knowing my object is result.AircraftList

jmargolisvt
  • 5,722
  • 4
  • 29
  • 46
Jason
  • 27
  • 1
  • 6
  • Not a dupe as I have an array of arrays. – Jason Dec 09 '16 at 22:12
  • Ok so the inner loop would be from that answer. The outer loop would be from this answer: [Loop through an array in javascript](http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – styfle Dec 09 '16 at 22:18
  • Loop through and do what with? –  Dec 10 '16 at 12:05

2 Answers2

0

You can iterate over the array using .forEach and then iterate over the properties using a for in loop inside the previous loop.

var data = {"status":200,"status_message":"Valid Account","AircraftList":[{"ID":"1","FullTXT":"Boeing 777-200ER","TypeCode":"772","Manufacturer":"Boeing","Model":"777","Variant":"200ER","PaxCnt":"305","RangeNM":"5240","MinRwFT":"8000","Cost":"261500000","DeliveryDelay":"18"},{"ID":"2","FullTXT":"Airbus A320-200","TypeCode":"320","Manufacturer":"Airbus","Model":"A320","Variant":"200","PaxCnt":"186","RangeNM":"3300","MinRwFT":"2100","Cost":"98000000","DeliveryDelay":"9"}]}

var MyAircraftList = data.AircraftList;

MyAircraftList.forEach(function(aircraft, i) {
  for (var key in aircraft) {
     console.log(i, key, aircraft[key]);
  }
});
styfle
  • 22,361
  • 27
  • 86
  • 128
  • I get the following error with your code - Uncaught TypeError: Cannot read property 'forEach' of undefined(…) – Jason Dec 10 '16 at 02:32
  • That means the variable you called `.forEach` on is undefined and not the expected array variable. – styfle Dec 12 '16 at 18:00
  • How can I resolve that? – Jason Dec 13 '16 at 23:28
  • I can't read your mind unfortunately. The JSON from your question works fine with my answer if you use "Run code snippet" button. I suggest you post a new question with the new JSON that is giving you an error. – styfle Dec 14 '16 at 14:14
0

I would use a simple recursive function to iterate through an object. It makes possible iterating through nested objects.

// Your response
var response = {
    "status": 200,
    "status_message": "Valid Account",
    "AircraftList": [{
            "ID": "1",
            "FullTXT": "Boeing 777-200ER",
            "TypeCode": "772",
            "Manufacturer": "Boeing",
            "Model": "777",
            "Variant": "200ER",
            "PaxCnt": "305",
            "RangeNM": "5240",
            "MinRwFT": "8000",
            "Cost": "261500000",
            "DeliveryDelay": "18"
        }, {
            "ID": "2",
            "FullTXT": "Airbus A320-200",
            "TypeCode": "320",
            "Manufacturer": "Airbus",
            "Model": "A320",
            "Variant": "200",
            "PaxCnt": "186",
            "RangeNM": "3300",
            "MinRwFT": "2100",
            "Cost": "98000000",
            "DeliveryDelay": "9"
        }]
};

Define somewhere this function:

function iterateRecursively(object, callbackFunction) {

    // Walk through object
    for (var keyName in object) {

        // If the given parameter is an object, call the function over and 
        // over again, till you get a string or number
        if (typeof object[keyName] === 'object') {

            iterateRecursively(object[keyName], callbackFunction);

        } else {

            // Callback function to do something with results outside of main function
            if (typeof callbackFunction === 'function') {
                callbackFunction(keyName, object[keyName]);
            }

        }
    }
}

So, now you're able to walk through sub elements by using:

iterateRecursively(response.AircraftList, function (key, value) {

    // You could use "status" as condition. For example if you get some other status 
    // than "200", you could do something else (may be an error message).
    // For example: 
    // if(key == 'status' && value != 200) doSomething();

    console.log(key + " = " + value);
});

You also can pass entire "response" object to the function. It will has same effect. Additionally you'll get access to other elements like "status_message" etc.

Hitmacher
  • 1
  • 2
  • This does not work? Why? Am i not getting a valid array or object? – Jason Dec 11 '16 at 08:31
  • Strange. What error message do you get? What does not work exactly? I modified the function a bit to have easy access to its functionality outside of the function through a callback function. See working example: https://jsfiddle.net/Lqs3o2ry/ – Hitmacher Jan 04 '17 at 13:22