簡體   English   中英

循環通過嵌套在 Node.js 中的 Object

[英]Loop Through Nested Object in Node.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"
                        }
                    },
                }

            ]
        }
    ]

}

就目前而言,我發現很難訪問段數組中的數據。

這是你需要的嗎? (我正在考慮您的響應存儲在一個名為data的變量中)

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

origin_destinationssegments在您的數據中都是 arrays。

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

這就是我的響應數據的樣子

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

如果您更喜歡使用功能較少的方法,您也可以編寫如下代碼,您可以在瀏覽器上運行它。

 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)

我的一個朋友幫我解決了這個問題....下面是他的代碼片段。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM