遍历节点中的嵌套对象.js

请有人可以帮助或指导我写资源如何解决这个问题。在我的节点应用程序中,我正在发出API POST请求以访问公共飞行结果。我的回复数据如下所示:


 {

    "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"

                        }

                    },

                }


            ]

        }

    ]


}

就目前而言,我发现很难访问段数组中的数据。


神不在的星期二
浏览 216回答 4
4回答

尚方宝剑之说

这是你需要的吗?(我正在考虑您的响应存储在一个名为data)data.origin_destinations.forEach(destination => {  destination.segments.forEach(segment => {    console.log(segment);  });});两者都是数据中的数组。origin_destinationssegmentsES5 语法中的相同解决方案:data.origin_destinations.forEach(function(destination) {  destination.segments.forEach(function(segment) {    console.log(segment);  });});请参阅下面的运行代码段: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);  });});

梵蒂冈之花

如果您更喜欢使用功能较少的方法,也可以编写类似于以下代码的代码,您可以在浏览器上运行它。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)

隔江千里

这是我的响应数据的样子"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"                    }                }            ]        }]    }]}我用它来解构返回数据 const {            data: {                body: {                    data: {                        itineraries: [...origin_destinations]                    }                }            }        } = resp;

慕哥9229398

我的一个朋友帮我解决了这个问题。下面是他的代码片段。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);    });  });});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript