javascript循环显示子字段的数组

时间:2016-07-29 10:07:58

标签: javascript jquery

我正在尝试循环并获取下面数组中的位置名称。

{
   "id":"100",
   "name":"the name",
   "places":[
      {
         "id":"23",
         "name":"first place",
         "startDate":"2015-01-30 15:01:00",
         "endDate":"2015-01-30 17:01:00"
      },

      {
         "id":"54",
         "name":"second place",
         "startDate":"2015-01-31 17:01:00",
         "endDate":"2015-02-01 17:01:00"

      },
      {
         "id":"400",
         "name":"third place",
         "startDate":"2015-02-01 17:01:00",
         "endDate":"2015-02-05 17:01:00"

      }

   ]

}

所以这就是我所做的:

for (var i = 0; i < mainData.length; i++) {

     var obj = mainData.places[i];

     console.log(obj.places[i].name);
}

上面的代码显示未定义,所以代码中有错误。

如何让console.log显示所有地名?

3 个答案:

答案 0 :(得分:3)

请查看Nina Scholz的答案,因为帮助了解您的错误。

这是另一种方法(使用map

var arr = {
    "id":"100",
        "name":"the name",
        "places":[
    {
        "id":"23",
        "name":"first place",
        "startDate":"2015-01-30 15:01:00",
        "endDate":"2015-01-30 17:01:00"
    },

    {
        "id":"54",
        "name":"second place",
        "startDate":"2015-01-31 17:01:00",
        "endDate":"2015-02-01 17:01:00"

    },
    {
        "id":"400",
        "name":"third place",
        "startDate":"2015-02-01 17:01:00",
        "endDate":"2015-02-05 17:01:00"

    }

]

}

var places = arr.places.map(function(e){
    return e.name;
});

console.log(places)

答案 1 :(得分:3)

您需要places的长度以及通过nameobj的正确访问权限。

var mainData = { "id": "100", "name": "the name", "places": [{ "id": "23", "name": "first place", "startDate": "2015-01-30 15:01:00", "endDate": "2015-01-30 17:01:00" }, { "id": "54", "name": "second place", "startDate": "2015-01-31 17:01:00", "endDate": "2015-02-01 17:01:00" }, { "id": "400", "name": "third place", "startDate": "2015-02-01 17:01:00", "endDate": "2015-02-05 17:01:00" }] },
    obj;

for (var i = 0; i < mainData.places.length; i++) {
//                          ^^^^^^^^
     obj = mainData.places[i];
     console.log(obj.name);
     //          ^^^^^^^^
}

答案 2 :(得分:1)

在您的代码中obj是对内部对象的引用,因此从中获取name属性。虽然您需要检查palces属性(包含数组)的长度。

for (var i = 0; i < mainData.palces.length; i++) {
    //----------------------^^^^^^^^-------------
    var obj = mainData.places[i];
    console.log(obj.name);
    //------------^^^^^^^^--------
}

&#13;
&#13;
var mainData = {
  "id": "100",
  "name": "the name",
  "places": [{
    "id": "23",
    "name": "first place",
    "startDate": "2015-01-30 15:01:00",
    "endDate": "2015-01-30 17:01:00"
  }, {
    "id": "54",
    "name": "second place",
    "startDate": "2015-01-31 17:01:00",
    "endDate": "2015-02-01 17:01:00"

  }, {
    "id": "400",
    "name": "third place",
    "startDate": "2015-02-01 17:01:00",
    "endDate": "2015-02-05 17:01:00"

  }]
};
for (var i = 0; i < mainData.places.length; i++) {
  var obj = mainData.places[i];
  console.log(obj.name);
  //------------^^^^^^^^--------
}
&#13;
&#13;
&#13;

您可以使用 Array#forEach 方法更简单地迭代数组元素。

mainData.places.forEach(function(v){
    console.log(v.name);
})

&#13;
&#13;
var mainData = {
  "id": "100",
  "name": "the name",
  "places": [{
    "id": "23",
    "name": "first place",
    "startDate": "2015-01-30 15:01:00",
    "endDate": "2015-01-30 17:01:00"
  }, {
    "id": "54",
    "name": "second place",
    "startDate": "2015-01-31 17:01:00",
    "endDate": "2015-02-01 17:01:00"

  }, {
    "id": "400",
    "name": "third place",
    "startDate": "2015-02-01 17:01:00",
    "endDate": "2015-02-05 17:01:00"

  }]
};


 mainData.places.forEach(function(v) {
  console.log(v.name);
})
&#13;
&#13;
&#13;

如果您想将名称作为数组使用 Array#map 方法。

var res = mainData.places.map(function(v){
    return v.name;
})

&#13;
&#13;
var mainData = {
  "id": "100",
  "name": "the name",
  "places": [{
    "id": "23",
    "name": "first place",
    "startDate": "2015-01-30 15:01:00",
    "endDate": "2015-01-30 17:01:00"
  }, {
    "id": "54",
    "name": "second place",
    "startDate": "2015-01-31 17:01:00",
    "endDate": "2015-02-01 17:01:00"

  }, {
    "id": "400",
    "name": "third place",
    "startDate": "2015-02-01 17:01:00",
    "endDate": "2015-02-05 17:01:00"

  }]
};


var res = mainData.places.map(function(v) {
  return v.name;
})

console.log(res);
&#13;
&#13;
&#13;