通过使用Javascript和下划线嵌套对象和数组来循环

时间:2016-04-20 17:29:48

标签: javascript arrays json underscore.js

我正在尝试访问嵌套在数组中的对象。我从这个JSON对象(它是从XML数据库输出派生)开始:

{"report":
        {"date":"15 Apr 2016",
         "metrics":
            {"metric":
               [
                {"name":"Bank Angle",
                 "display_parent_group":"Bankfull",
                 "display_child_group":"SiteShape",
                 "tolerance":"0.05",
                 "visits":
                  {"visit":
                   [
                    {"visit_id":"3047","value": "0.47"},
                    {"visit_id":"2164","value": "0.55"},
                    {"visit_id":"1568","value": "0.72"},
                    {"visit_id":"3431","value": "0.12"},
                    {"visit_id":"2428","value": "0.44"},
                    {"visit_id":"1567","value": "0.49"}
                   ]}},
                {"name":"Bank Angle SD",
                 "display_parent_group":"Bankfull",
                 "display_child_group":"SiteShape",
                 "tolerance":"0.05",
                 "visits":
                 {"visit":
                  [
                   {"visit_id":"3047","value": "0.12"},
                   {"visit_id":"2164","value": "0.05"},
                   {"visit_id":"1568","value": "0.21"},
                   {"visit_id":"3431","value": "0.68"},
                   {"visit_id":"2428","value": "0.22"},
                   {"visit_id":"1567","value": "0.13"}
                  ]}},
               {"name":"Bankfull Area",
                "display_parent_group":"Bankfull",
                "display_child_group":"SiteSize","tolerance":"0.05",
                "visits":
                 {"visit":
                  [
                   {"visit_id":"3047","value": "202"},
                   {"visit_id":"2164","value": "193"},
                   {"visit_id":"1568","value": "115"},
                   {"visit_id":"3431","value": "258"},
                   {"visit_id":"2428","value": "89"},
                   {"visit_id":"1567","value": "206"}
                  ]}}
               ]
             }
          }
}

然后我使用下划线来提取metric个对象的子集:

var table_metric = JSONData.report.metrics.metric;

var target_metrics = _.where(table_metric, {
                  display_parent_group : 'Bankfull', display_child_group: 'SiteShape'
                  });

这会产生一个包含两个嵌套对象的数组。我遇到问题的地方是访问嵌套在visits.visit内的对象数组。例如,如果我想构建一个与键visit_id关联的值数组,我尝试:

function buildVisitIDArray(target_metrics) {
    var attrList = [];
    for(var i=0; i<target_metrics.length; i++) {
      var visit_records = target_metrics[i].visits[1];
      console.log(visit_records);
      for(visit_record in visit_records) {
        attrList.push(_.pluck(visit_record, "visit_id"));
    } 
}
return attrList

}

我得到了一系列未定义的结果。我花了几个小时尝试使用语法变体来获取嵌套的“访问”对象,但我似乎无法弄明白。

对于这位新手,非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

在你的buildVisitIDArray函数中,你试图让target_metrics [i] .visits [1]好像它是一个数组,但它实际上是一个对象,所以你应该这样使用它:

function buildVisitIDArray(target_metrics) {
    attrList = [];
    for(var i=0; i<target_metrics.length; i++) {
        var visit_records = target_metrics[i].visits; // Removed the array call ([1])
        console.log(visit_records);
        for(visit_record in visit_records) {
            attrList.push(_.pluck(visit_records[visit_record], "visit_id"));
        }
    }
    return attrList;
}

希望有所帮助:)

答案 1 :(得分:0)

如果您未在代码中的其他位置使用attrList关键字定义var,则可能会遇到问题。

根据Andre的回答,您可能希望将此行更改为:

visit_records = target_metrics[i].visits.visit;

深入一层,然后再进行常规数组for循环。