admin管理员组文章数量:1291007
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?
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?
2 Answers
Reset to default 9Will 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));
.
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']);
本文标签: javascriptangularjs counting object lengthStack Overflow
版权声明:本文标题:javascript - angularjs counting object length - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741498814a2381959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论