0

Please can someone help or direct me to the write resource on how to solve this. in my node app, i am making an API POST request to access a public flight results. my response data is shown below :

 {
    "origin_destinations": [
        {
            "ref_number": "0",
            "direction_id": "0",
            "elapsed_time": "2435",
            "segments": [
                {
                    "departure": {
                        "date": "2020-05-20",
                        "time": "20:45:00",
                        "airport": {
                            "code": "LOS",
                            "name": "Lagos-Murtala Muhammed Intl, Nigeria",
                            "city_code": "",
                            "city_name": "Lagos",
                            "country_code": "NG",
                            "country_name": "Nigeria",
                            "terminal": "I"
                        }
                    },
                }

            ]
        }
    ]

}

as it stands i am finding it difficult accessing the data in the segments Array.

4 Answers 4

1

Is this what you need? (I'm considering your response is stored in a variable called data)

data.origin_destinations.forEach(destination => {
  destination.segments.forEach(segment => {
    console.log(segment);
  });
});

Both origin_destinations and segments are arrays in your data.

The same solution in ES5 syntax:

data.origin_destinations.forEach(function(destination) {
  destination.segments.forEach(function(segment) {
    console.log(segment);
  });
});

See the running snippet below:

var data = {
    "origin_destinations": [
        {
            "ref_number": "0",
            "direction_id": "0",
            "elapsed_time": "2435",
            "segments": [
                {
                    "departure": {
                        "date": "2020-05-20",
                        "time": "20:45:00",
                        "airport": {
                            "code": "LOS",
                            "name": "Lagos-Murtala Muhammed Intl, Nigeria",
                            "city_code": "",
                            "city_name": "Lagos",
                            "country_code": "NG",
                            "country_name": "Nigeria",
                            "terminal": "I"
                        }
                    },
                }

            ]
        }
    ]

};

data.origin_destinations.forEach(function(destination) {
  destination.segments.forEach(function(segment) {
    console.log(segment);
  });
});

Sign up to request clarification or add additional context in comments.

5 Comments

I tried the code snipped you provided but i was still not able to access the values in the segments array. the second forEach loop returned undefined
@promiselxg Could you replicate your problem at jsbin.com ? You can save it there and paste the link here for us to check your code.
here is the link https://jsbin.com/nuladituni/edit?js,console, i duplicated the origin destinations and looped through it using your code snipped, but it only returned the first index in that origin_destinations array
i can as well drop the link to the API documentation, if you don't mind
@promiselxg You duplicated the key "origin_destinations", so the second will replace the first. If the data comes like that from your source there is a problem in the API you are consuming. This is a working example: jsbin.com/jopizoyafo/edit?js,console My change is explained in comments in the code. And yes, you can paste the link to the API. Please add it to your question (there is an edit button below it) :)
0

this is how my response data looks like

"data": {
    "itineraries": [{
        "origin_destinations":[{
            "segments":[
                "departure":{
                    "airport": {
                        "code": "LOS",
                        "name": "Lagos-Murtala Muhammed Intl, Nigeria",
                        "city_code": "",
                        "city_name": "Lagos",
                        "country_code": "NG",
                        "country_name": "Nigeria",
                        "terminal": "I"
                    }
                }
            ]
        }]
    }]
}

i used this to de-structure the return data

 const {
            data: {
                body: {
                    data: {
                        itineraries: [...origin_destinations]
                    }
                }
            }
        } = resp;

Comments

0

If you prefer to use a less functional approach you could also write the code like the following code that you can run it right here on the browser.

function processData(data) {
  for (const originDestination of data.origin_destinations) {
    const {
      ref_number,
      direction_id,
      elapsed_time,
      segments
    } = originDestination
    console.log({
      ref_number,
      direction_id,
      elapsed_time
    })

    for (const segment of segments) {
      const {
        departure
      } = segment

      console.log(departure)
    }
  }
}

const data = {
  "origin_destinations": [{
    "ref_number": "0",
    "direction_id": "0",
    "elapsed_time": "2435",
    "segments": [{
      "departure": {
        "date": "2020-05-20",
        "time": "20:45:00",
        "airport": {
          "code": "LOS",
          "name": "Lagos-Murtala Muhammed Intl, Nigeria",
          "city_code": "",
          "city_name": "Lagos",
          "country_code": "NG",
          "country_name": "Nigeria",
          "terminal": "I"
        }
      }
    }]
  }]
}

processData(data)

3 Comments

thanks for your help, but unfortunately when i ran the code snippet ``segments` returned undefined even ref_number, direction_id and elapsed_time
If you are running one of the latest versions of Chrome or Firefox it will work. I just tested on 3 different computers and they all worked for me. You can copy that code and save it as sample-solution.js and then run it with node.js by typing node sample-solution.js that should work.
thanks for your help, i really appreciate...i have tried it as instructed and it worked, please is there a way of making this work like sample solution?
0

a friend of mine helped me solve the problem....below is his code snippet.

var data = [
   {
      "origin_destinations":[
         {
            "ref_number":"0",
            "direction_id":"0",
            "elapsed_time":"2435",
            "segments":[
               {
                  "departure":{
                     "date":"2020-05-20",
                     "time":"20:45:00",
                     "airport":{
                        "code":"LOS",
                        "name":"Lagos-Murtala Muhammed Intl, Nigeria",
                        "city_code":"",
                        "city_name":"Lagos",
                        "country_code":"NG",
                        "country_name":"Nigeria",
                        "terminal":"I"
                     }
                  },
                  "arrival":{
                     "date":"2020-05-20",
                     "time":"20:45:00",
                     "airport":{
                        "code":"LOS",
                        "name":"Lagos-Murtala Muhammed Intl, Nigeria",
                        "city_code":"",
                        "city_name":"Lagos",
                        "country_code":"NG",
                        "country_name":"Nigeria",
                        "terminal":"I"
                     }
                  }
               }
            ]
         }
      ]
   }
];

data.forEach(function(row) {
  row.origin_destinations.forEach(function(destination) {
    destination.segments.forEach(function(segments) {
      console.log(segments.departure.airport.name);
    });
  });
});

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.