我在angularjs中有以下代码,但是当我想要我的数组0位置的长度时,它显示未定义请检查下面的代码,这就是为什么我的第二个循环不运行。在角度如何解决这类问题,请任何人帮助我
this.current_engineer = response.data;
var current_schedules = this.current.schedule;
console.log(current_schedules);
console.log(current_schedules.length);
console.log(current_schedules[0].length);
if (angular.isArray(current_schedules)) {
for (var j = 0; j < current_schedules.length; j++) {
for (var i = 0; i < current_schedules[j].length; i++) //this loop does not run because of length how to get length
{
console.log(current_schedules[j][i].title);
}
}
}
///------------Mongodb Database-----------------------
"schedule" : [
[
{
"_id" : ObjectId("58f76aa9be4d311a78f24f78"),
"sch_end" : ISODate("2017-04-21T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-21T00:00:00.000Z"),
"available" : false,
"title" : "test3"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f77"),
"sch_end" : ISODate("2017-04-22T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-22T00:00:00.000Z"),
"available" : false,
"title" : "test4"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f76"),
"sch_end" : ISODate("2017-04-23T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-23T00:00:00.000Z"),
"available" : false,
"title" : "test6"
}
],
[
{
"_id" : ObjectId("58f76aa9be4d311a78f24f78"),
"sch_end" : ISODate("2017-04-24T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-24T00:00:00.000Z"),
"available" : false,
"title" : "test9"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f77"),
"sch_end" : ISODate("2017-04-25T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-25T00:00:00.000Z"),
"available" : false,
"title" : "test10"
},
{
"_id" : ObjectId("58f76aa9be4d311a78f24f76"),
"sch_end" : ISODate("2017-04-27T00:00:00.000Z"),
"sch_start" : ISODate("2017-04-27T00:00:00.000Z"),
"available" : false,
"title" : "test11"
}
]
],
答案 0 :(得分:3)
您的属性current_schedules[0]
处的值是一个js对象,而js对象没有任何名为length的属性。您可以使用类似Object.keys(current_schedules[0]).length
的内容。
注意:适用于IE9 +和所有其他支持ES5 +的现代浏览器。