Javascript 如何计算AngularJs对象中的合计值和空值

Javascript 如何计算AngularJs对象中的合计值和空值,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我试图从angularjs对象中的total字段中计算出totalnot null字段,然后我想计算百分比 例如: 在“我的对象”中包含10个字段。两个字段有一些值(其他null) Completed=(非空字段/总字段)*100 =(4/11)*100=36.36% 我的控制器 myApp.controller('PController', function ($scope,localStorageService) { $scope.user = localStorageService.ge

我试图从angularjs对象中的
total
字段中计算出total
not null
字段,然后我想计算百分比

例如:

在“我的对象”中包含10个字段。两个字段有一些值(其他
null

Completed=(非空字段/总字段)*100
=(4/11)*100=36.36%

我的控制器

myApp.controller('PController', function ($scope,localStorageService) {

$scope.user = localStorageService.get("user");

console.log(Object.keys($scope.user).length);
});
现在我可以得到字段总数。但是如何计算
notnull
字段和调用百分比

$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

它显示了
“arrow function syntax(=>)”仅在ES6 angularjs中可用
是的,您需要使用babel将ES6传输到ES5,或者用常规函数替换=>,这样它将成为Object.keys($scope.user).filter(函数(x){return$scope.user[x]!==null})或者,如果它的ESLint警告使用ESLint config在编辑器中为ES6启用ESLint是,这是有帮助的。但您必须在末尾添加
长度
。无论如何,这都是很大的帮助。