angularjs counting object length

ε祈祈猫儿з 提交于 2019-12-20 03:53:14

问题


I'm using underscore to count object length. _.size(object). Because this object is being handled by angularjs there's a $$hashKey property in the object that's making the length 1 larger than it should be. What's the correct way to count object lengths in angularjs?


回答1:


Will this do? _.size(_.omit(object, '$$hashKey'));

Updated

angular.copy() strips $$hashKey out for you. So it seems a more Angular way would be _.size(angular.copy(object));.




回答2:


What about _.size(angularObject) - 1; ?

If this isn't enough, you can of course create your own size function that dosen't count the $$hashKey:

_.extend(_, { 
   mySize: function(collection, ignored) {
    var size = 0,

    _.each(collection, function(value, key) {
      if (!_.contains(ignored, key)) {
        size++;
      }
    });

    return size;
  } 
});

Example:

var len = _.mySize(angularObject, ['$$hashKey']);


来源:https://stackoverflow.com/questions/17335108/angularjs-counting-object-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!