遍历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_destinations
和segments
都是数据中的数组。
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中的嵌套对象
发布评论