遍历Node.js中的嵌套对象

编程入门 行业动态 更新时间:2024-10-09 12:28:55

<a href=https://www.elefans.com/category/jswz/34/1771029.html style=遍历Node.js中的嵌套对象"/>

遍历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"
                        }
                    },
                }

            ]
        }
    ]

}

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

回答如下:

这是您需要的吗? (我考虑您的响应存储在名为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);
  });
});

更多推荐

遍历Node.js中的嵌套对象

本文发布于:2024-05-07 20:46:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   嵌套   对象   js   Node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!