Search code examples
angularjsmongodbangularjs-directiveangularjs-scope

how to get length in angularjs because of length my second loop not run


i have below code in angularjs but when i want length of my array 0 position its show undefined please check below code that's why my second loop does not run. in angular how to solve this type of problem anyone help me please

        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"
            }
        ]
    ], 

enter image description here


Solution

  • Your property's value at current_schedules[0] is a js object and js object doesn't have any property called length. You could use something like Object.keys(current_schedules[0]).length.

    Note: For IE9+ and all other modern ES5+ capable browsers.