Javascript 遍历Json并仅返回一些属性

Javascript 遍历Json并仅返回一些属性,javascript,json,node.js,object,Javascript,Json,Node.js,Object,是否可以搜索对象属性并在同一级别访问所有属性 我有步骤对象,它可以从1到n个属性(方向步骤)进行余弦。我只想选择具有相同级别值的属性: "travel_mode" : "TRANSIT" 这意味着我可以访问例如name属性,该属性在travkl_模式行走的级别中不可用 "steps" : [ { "distance" : {

是否可以搜索对象属性并在同一级别访问所有属性

我有步骤对象,它可以从1到n个属性(方向步骤)进行余弦。我只想选择具有相同级别值的属性:

"travel_mode" : "TRANSIT"
这意味着我可以访问例如name属性,该属性在travkl_模式行走的级别中不可用

                 "steps" : [
                    {
                       "distance" : {
                          "text" : "0,3 km",
                          "value" : 285
                       },
                       "travel_mode" : "WALKING"
                    },
                    {
                       "distance" : {
                          "text" : "13 m",
                          "value" : 13
                       },
                       "travel_mode" : "WALKING"
                    }
                 ],
                 "travel_mode" : "WALKING"
              },
              {
                 "distance" : {
                    "text" : "2,5 km",
                    "value" : 2506
                 },
                    "departure_stop" : {
                       "location" : {
                          "lat" : 48.1157011,
                          "lng" : 11.6602402
                       },
                       "name" : "Mönchbergstraße"
                 },
                 "travel_mode" : "TRANSIT"
              },
              {
                 "distance" : {
                    "text" : "0,2 km",
                    "value" : 230
                 },
                       "travel_mode" : "WALKING"
                    },
                    {
                       "distance" : {
                          "text" : "15 m",
                          "value" : 15
                       },
                       "duration" : {
                          "text" : "1 Minute",
                          "value" : 16
                       },
                       "end_location" : {
                          "lat" : 48.1175728,
                          "lng" : 11.6371798
                       },
                       "html_instructions" : "\u003cb\u003eLinks\u003c/b\u003e Richtung \u003cb\u003eHeinrich-Wieland-Straße\u003c/b\u003e abbiegen",
                       "maneuver" : "turn-left",
                       "polyline" : {
                          "points" : "u~tdHu{_fAZH"
                       },
                       "start_location" : {
                          "lat" : 48.1177065,
                          "lng" : 11.6372328
                       },
                       "travel_mode" : "WALKING"
                    },
                    {
                       "distance" : {
                          "text" : "35 m",
                          "value" : 35
                       },
                       "duration" : {
                          "text" : "1 Minute",
                          "value" : 24
                       },
                       "end_location" : {
                          "lat" : 48.1176498,
                          "lng" : 11.6367222
                       },
                       "html_instructions" : "\u003cb\u003eRechts\u003c/b\u003e abbiegen auf \u003cb\u003eHeinrich-Wieland-Straße\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDas Ziel befindet sich auf der linken Seite.\u003c/div\u003e",
                       "maneuver" : "turn-right",
                       "polyline" : {
                          "points" : "y}tdHk{_fAAHK|@AR"
                       },
                       "start_location" : {
                          "lat" : 48.1175728,
                          "lng" : 11.6371798
                       },
                       "travel_mode" : "WALKING"
                    }
                 ],
                 "travel_mode" : "WALKING"
              }
           ],
           "traffic_speed_entry" : [],
           "via_waypoint" : []
        }
     ],
我试过这样的方法:

        var answer = JSON.parse(body), busName;

        for(key in answer){                               
            if(key.indexOf("name") != -1){             
                busName = answer[key];                
                break;                                  
            }
        }

但是我在busName中得到了未定义的值

我将尝试帮助您使用经过验证的json

var answer = {
  "steps": [
    {
    "distance": {
      "text": "0,3 km",
      "value": 285
    },
    "travel_mode": "WALKING"
  },
  {
    "distance": {
      "text": "13 m",
      "value": 13
    },
    "travel_mode": "WALKING"
  },
  {
    "distance": {
      "text": "2,5 km",
      "value": 2506
    },
    "departure_stop": {
      "location": {
        "lat": 48.1157011,
        "lng": 11.6602402
      },
      "name": "Mönchbergstraße"
    },
    "travel_mode": "TRANSIT"
  },
  {
    "distance": {
      "text": "0,2 km",
      "value": 230
    },
    "travel_mode": "WALKING"
  },
  {
    "distance": {
      "text": "15 m",
      "value": 15
    },
    "duration": {
      "text": "1 Minute",
      "value": 16
    },
    "end_location": {
      "lat": 48.1175728,
      "lng": 11.6371798
    },
    "html_instructions": "<b>Links</b> Richtung <b>Heinrich-Wieland-Straße</b> abbiegen",
    "maneuver": "turn-left",
    "polyline": {
      "points": "u~tdHu{_fAZH"
    },
    "start_location": {
      "lat": 48.1177065,
      "lng": 11.6372328
    },
    "travel_mode": "WALKING"
  },
  {
    "distance": {
      "text": "35 m",
      "value": 35
    },
    "duration": {
      "text": "1 Minute",
      "value": 24
    },
    "end_location": {
      "lat": 48.1176498,
      "lng": 11.6367222
    },
    "html_instructions": "<b>Rechts</b> abbiegen auf <b>Heinrich-Wieland-Straße</b><div style=\"font-size:0.9em\">Das Ziel befindet sich auf der linken Seite.</div>",
    "maneuver": "turn-right",
    "polyline": {
      "points": "y}tdHk{_fAAHK|@AR"
    },
    "start_location": {
      "lat": 48.1175728,
      "lng": 11.6371798
    },
    "travel_mode": "WALKING"
  }
 ],
 "traffic_speed_entry": [],
 "via_waypoint": []
};
以上假设所有离开停止对象都有一个“name”键。如果没有,在尝试访问其值之前,您也应该对其执行空检查

for (key in answer.steps) {
  if (answer.steps[key].travel_mode && answer.steps[key].travel_mode === "TRANSIT") {

    // do what you want with the answer.steps[key] object here
    console.log(answer.steps[key].departure_stop.name); // ​​​​​Mönchbergstraße
  }
}

我希望这会有所帮助,目前还不清楚您到底想实现什么。

我认为您的json无效。我把它粘贴到了一个文件夹中,我不得不猜测你可能想让它验证什么。在你开始尝试访问密钥之前,你必须先弄清楚这一点。如果你只是控制台。日志(回答)你得到了什么?我不确定你想问什么。。。。你说的是每一个旅行模式为“运输”的物体,你想访问站点名称吗?或者对于任何对象中的任何移动模式,是否要访问任何其他对象的名称?
for (key in answer.steps) {
  if (answer.steps[key].travel_mode && answer.steps[key].travel_mode === "TRANSIT") {

    // do what you want with the answer.steps[key] object here
    console.log(answer.steps[key].departure_stop.name); // ​​​​​Mönchbergstraße
  }
}