Get the object length in angularjs is undefined

后端 未结 8 2292
梦谈多话
梦谈多话 2021-02-19 03:57

How can I get the object length ?

In console my object looks like this:

Object {
 2: true,
 3: true,
 4: true
}

.length w

8条回答
  •  时光说笑
    2021-02-19 04:39

    You could also use a filter

    Edit:

    app.filter('objLength', function() { 
     return function(object) { 
      return Object.keys(object).length; 
     } 
    });
    

    Old Answer:

    app.filter('objLength', function() {
     return function(object) {
      var count = 0;
    
      for(var i in object){
       count++;
      }
      return count;
     }
    });
    

    And then use it maybe like this

    content

提交回复
热议问题