为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

神不在的星期二 2022-08-27 10:50:16
请有人可以帮助或指导我写资源如何解决这个问题。在我的节点应用程序中,我正在发出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"                        }                    },                }            ]        }    ]}就目前而言,我发现很难访问段数组中的数据。
查看完整描述

4 回答

?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

这是你需要的吗?(我正在考虑您的响应存储在一个名为data)


data.origin_destinations.forEach(destination => {

  destination.segments.forEach(segment => {

    console.log(segment);

  });

});

两者都是数据中的数组。origin_destinationssegments


ES5 语法中的相同解决方案:


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);

  });

});


查看完整回答
反对 回复 2022-08-27
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

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


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)


查看完整回答
反对 回复 2022-08-27
?
隔江千里

TA贡献1906条经验 获得超10个赞

这是我的响应数据的样子


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


查看完整回答
反对 回复 2022-08-27
?
慕哥9229398

TA贡献1877条经验 获得超6个赞

我的一个朋友帮我解决了这个问题。下面是他的代码片段。


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);

    });

  });

});


查看完整回答
反对 回复 2022-08-27
  • 4 回答
  • 0 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信