gpt4 book ai didi

javascript - firebase 对象大小 : how to ignore properties with $?

转载 作者:行者123 更新时间:2023-11-30 17:05:48 24 4
gpt4 key购买 nike

我们有一个包含用户通知数据的对象。我们如何忽略所有以 $ 开头的 firebase 键?因为在用

计算对象的大小时
.filter('keylength', function() {
return function(input) {
if (!angular.isObject(input)) {
throw Error("input was not an object!")
}
return Object.keys(input).length;
}
})

它返回“错误”的数字(我知道它是正确的,但在我们的例子中......)该对象看起来像这样:

$$conf: Object
$id: "notifications"
$priority: null
$value: null
__proto__: Object

过滤器返回“1”,应该返回0,因为对象没有子节点

最佳答案

您可以在过滤器中过滤掉它们。

.filter('keylength', function() {
var ignoreExp = /^\$+/;
return function(input) {
if (!angular.isObject(input)) {
throw Error("input was not an object!")
}

return Object.keys(input).filter(function(key) {
//This is what angular does in angular.copy
//return (key.charAt(0) !== '$' && key.charAt(1) !== '$');
//Or just Filter out the ones that starts with any number of $
return !ignoreExp.test(key);
}).length;
}
});

演示

angular.module('app', []).controller('ctrl', function($scope) {

$scope.obj = {
$$conf: {},
$id: "notifications",
$priority: null,
$$value: null,
test:"hey"
};
}).filter('keylength', function() {
var ignoreExp = /^\$+/;
return function(input) {
if (!angular.isObject(input)) {
throw Error("input was not an object!")
}

return Object.keys(input).filter(function(key) {
//return (key.charAt(0) !== '$' && key.charAt(1) !== '$');
return !ignoreExp.test(key)
}).length;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
Length={{obj|keylength}}
</div>

关于javascript - firebase 对象大小 : how to ignore properties with $?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28072819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com