Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何找到过滤数组的长度?_Angular - Fatal编程技术网

Angular 如何找到过滤数组的长度?

Angular 如何找到过滤数组的长度?,angular,Angular,这是我的数据库集 { "_id" : ObjectId("59a52c536851fc3289875d14"), "device_id" : "ASDFGHJ1234567", "users" : [ { "user_name" : "Athira", "player_level" : "beginner", "created_on" : "2017-01-25 09:54:40",

这是我的数据库集

{
    "_id" : ObjectId("59a52c536851fc3289875d14"),
    "device_id" : "ASDFGHJ1234567",
    "users" : [ 
        {
            "user_name" : "Athira",
            "player_level" : "beginner",
            "created_on" : "2017-01-25 09:54:40",
            "updated_on" : "2017-01-25 09:54:40",
            "status" : "Y"
        }, 
        {
            "user_name" : "Sandeep",
            "player_level" : "beginner",
            "created_on" : "2017-01-25 09:54:40",
            "updated_on" : "2017-01-25 09:54:40",
            "status" : "D"
        }
    ],
    "device_created_on" : "2017-01-25 09:54:40",
    "device_status" : "Y"
}
我需要列出如下所示的设备和用户

Device             No: users    Status
 ASDFGHJ1234567       2            Y
在这里,我使用了NGF来显示这个

<table border="1" cellpadding="5" cellspacing="3">
   <tr>
   <td>Device Id</td>
   <td>Number of Users</td>
   <td>Status</td>
   <td>Action</td>
   </tr>
   <tr *ngFor="let hero of users">
     <td>{{ hero.device_id}}</td>
     <td>{{ hero.users?.length }}</td>
     <td>{{ hero.device_status }}</td>
   </tr>
   </table>      

设备Id
用户数
地位
行动
{{hero.device_id}
{{hero.users?.length}
{{hero.device_status}
但我只需要统计那些状态为“Y”的用户。这里只有user_name=“Athira”应该计算在内

我如何实现这一点


谢谢

您可以像这样过滤阵列:

getCount(hero: any): number {
 if (!hero || !hero.users) return 0;

 return hero.users.filter(u => u.status === 'Y').length;
}

您可以按如下方式筛选阵列:

getCount(hero: any): number {
 if (!hero || !hero.users) return 0;

 return hero.users.filter(u => u.status === 'Y').length;
}

你也可以这样做。在ts文件中,让arrData作为您的响应

   var data = this.arrData.users;

   var count = 0; var i=0;

   data.forEach(element => {
       if(data[i].status == "Y"){
        count++;
       }
       i++;
   });

   console.log(count); //you will get count value which is users with status 'Y'

你也可以这样做。在ts文件中,让arrData作为您的响应

   var data = this.arrData.users;

   var count = 0; var i=0;

   data.forEach(element => {
       if(data[i].status == "Y"){
        count++;
       }
       i++;
   });

   console.log(count); //you will get count value which is users with status 'Y'

compiler.es5.js:1690未捕获错误:模板解析错误:解析器错误:绑定不能在ng:///AppModule/testComponent中[{{hero?.users?.filter(u=>u.status=='Y').length}]的第25列包含赋值。html@22:39({hero.device_id}}[ERROR->]{{{hero?.users?.filter(u=>u.status=='Y').length}您在typescript中的函数中尝试过吗?我不确定angular是否可以在htmlcompiler.es5.js:1690未捕获错误:模板解析错误:解析器错误:绑定不能包含[{{hero?.users?.filter(u=>u.status==''Y').length}]中第25列的赋值在ng:///AppModule/testComponent中。html@22:39(“{hero.device_id}}[ERROR->]{{hero?.users?.filter(u=>u.status==='Y').length}}您是否在您的typescript中的函数中尝试过它?我不确定angular是否可以在html中处理此问题