json/rest api:如何获取与特定值匹配的节点数 [英] json/rest api: how to get the count of nodes matching the specific value

查看:37
本文介绍了json/rest api:如何获取与特定值匹配的节点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在db.json中有数据(4000多个记录).我正在使用有角度的httpclient来显示数据(使用CRUD操作).

I have data in db.json (4000+ records). I am using angular httpclient to display the data (using CRUD operations).

其中一列是性别.现在,我想知道男性,女性和其他人的人数.

One of the columns is Gender. Now, I wanted to know the count of males, females, and others.

我需要在打字稿中写些什么才能知道所有记录中男性,女性以及其他人的数量和显示数量?

What do I need to write in my typescript to know the count and display number of males, females, and others out of all records?

app.component.html

app.component.html

{{impactCount.applicable}}  
{{impactCount.fyi}}

app.component.ts

app.component.ts

impactCount = {
    applicable: 0,
    fyi: 0
  }

     getLatestUser() {
    this.commonService.getAllUser().subscribe((response) => {
      this.allUser = response;
      this.totalRecords = this.allUser.length;
      this.allUser.forEach(row => this.impactCount[row.impact]++);
    })
  }

谢谢

推荐答案

您可以做一个简单的循环并添加另外两个名为 maleCount femaleCount

You can do a simple loop and add two more variables called maleCount and femaleCount

genderCount = {
    male: 0;
    female: 0;
    other: 0;
}

getLatestUser() { 
    this.service.getAllUser().subscribe((response) => { 
        this.allUser = response; 
        this.totalRecords = this.allUser.length;

        this.allUser.forEach(row => {
            if (row.gender === 'Male') {
                this.genderCount.male++;
            } else if (row.gender === 'Female') {
                this.genderCount.female++;
            } else {
                this.genderCount.other++;
            }
        });
    }); 
}

或者,如果数据库中的性别选项将是"male","female"和"other",则可以像这样访问它们并减少代码.

Alternatively, if the gender option in the db is going to be 'male', 'female', and 'other', you can access them like this and cut down on code.

this.allUser.forEach(row => this.genderCount[row.gender]++);

这篇关于json/rest api:如何获取与特定值匹配的节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆