如何计算 AngularJs 对象中的总值和空值
How to count total and null values in AngularJs object
我正在尝试计算 angularjs 对象中 total
个字段的总数 not null
个字段,然后我想计算百分比。
例如:
在我的对象中包含 10 个 fields.two 字段有一些值(其他 null
)。
Completed = (not null fields/total fields)*100
= (4/11)*100 = 36.36%
我的控制器
myApp.controller('PController', function ($scope,localStorageService) {
$scope.user = localStorageService.get("user");
console.log(Object.keys($scope.user).length);
});
现在我可以获得总数 fields.but 我如何计算 not null
字段并计算百分比?
$scope.user对象如下
{
"user_id": "205",
"first_name": null,
"last_name": null,
"email": "at@yuib.com",
"address": null,
"mobile": null,
"phone": null,
"profile_img": null,
"gender": null,
"registered": "1",
"addresses": 0
}
在 $scope.user 上使用 Object.keys 的简单过滤方法应该会给出所需的结果
Object.keys($scope.user).filter(x=>$scope.user[x]!==null).length
试试这个:
Object.values($scope.user).filter((item) => item != null).length
我正在尝试计算 angularjs 对象中 total
个字段的总数 not null
个字段,然后我想计算百分比。
例如:
在我的对象中包含 10 个 fields.two 字段有一些值(其他 null
)。
Completed = (not null fields/total fields)*100
= (4/11)*100 = 36.36%
我的控制器
myApp.controller('PController', function ($scope,localStorageService) {
$scope.user = localStorageService.get("user");
console.log(Object.keys($scope.user).length);
});
现在我可以获得总数 fields.but 我如何计算 not null
字段并计算百分比?
$scope.user对象如下
{
"user_id": "205",
"first_name": null,
"last_name": null,
"email": "at@yuib.com",
"address": null,
"mobile": null,
"phone": null,
"profile_img": null,
"gender": null,
"registered": "1",
"addresses": 0
}
在 $scope.user 上使用 Object.keys 的简单过滤方法应该会给出所需的结果
Object.keys($scope.user).filter(x=>$scope.user[x]!==null).length
试试这个:
Object.values($scope.user).filter((item) => item != null).length