json JavaScript计数Jason数据特定值

68bkxrlz  于 2023-07-01  发布在  Java
关注(0)|答案(1)|浏览(146)

这是我从服务器得到的数据,主要是统计“状态”部分,有多少是“alert”,“NOT_COMMUNICATING”或“”(空表示好)

Q:如何统计数据“status”,并显示“alert”为3,“NOT_COMMUNICATING”为5的数字排序?

我确实学习了JavaScript arr.reduce,但是数组有点棘手,我自己也搞不清楚,需要帮助,谢谢

  • 数据JSON
{
  "status": "success",
  "results": 20,
  "ipc_status": [
    {
      "device_id": "1001",
      "name": "E84 IoPC 11",
      "group": "Uk1",
      "ip": "192.168.10.10",
      "port": 5000,
      "ipc_enable": false,
      "ftp_enable": true,
      "status": "",
      "detail": ""
    },
    {
      "device_id": "1002",
      "name": "E3 IoPC 11",
      "group": "Uk1",
      "ip": "192.168.10.10",
      "port": 5006,
      "ipc_enable": true,
      "ftp_enable": true,
      "status": "NOT_COMMUNICATING",
      "detail": "NOT_COMMUNICATING"
    },
    {
      "device_id": "1003",
      "name": "E3 IoPC 15",
      "group": "Uk2",
      "ip": "192.168.10.10",
      "port": 5000,
      "ipc_enable": true,
      "ftp_enable": true,
      "status": "NOT_COMMUNICATING",
      "detail": "NOT_COMMUNICATING"
    },
    {
      "device_id": "1005",
      "name": "E3 IoPC 17",
      "group": "Uk3",
      "ip": "192.168.10.10",
      "port": 5051,
      "ipc_enable": false,
      "ftp_enable": false,
      "status": "",
      "detail": ""
    },
    {
      "device_id": "1010",
      "name": "E3 IoPC 21",
      "group": "Uk3",
      "ip": "192.168.10.10",
      "port": 5010,
      "ipc_enable": false,
      "ftp_enable": false,
      "status": "",
      "detail": ""
    },
    {
      "device_id": "1011",
      "name": "E3 IoPC 23",
      "group": "Uk5",
      "ip": "192.168.10.10",
      "port": 50610,
      "ipc_enable": false,
      "ftp_enable": false,
      "status": "",
      "detail": ""
    },
    {
      "device_id": "1012",
      "name": "E3 IoPC 25",
      "group": "Uk5",
      "ip": "192.168.10.10",
      "port": 50620,
      "ipc_enable": false,
      "ftp_enable": true,
      "status": "",
      "detail": ""
    },
    {
      "device_id": "1019",
      "name": "E3 IoPC 27",
      "group": "Uk5",
      "ip": "192.168.10.10",
      "port": 5069,
      "ipc_enable": false,
      "ftp_enable": true,
      "status": "",
      "detail": ""
    },
    {
      "device_id": "1004",
      "name": "E3 IoPC 29",
      "group": "Uk7",
      "ip": "192.168.10.10",
      "port": 5005,
      "ipc_enable": true,
      "ftp_enable": true,
      "status": "NOT_COMMUNICATING",
      "detail": "NOT_COMMUNICATING"
    },
    {
      "device_id": "test--023",
      "name": "string",
      "group": "Uk7",
      "ip": "string",
      "port": 0,
      "ipc_enable": false,
      "ftp_enable": true,
      "status": "",
      "detail": ""
    },

    {
      "device_id": "test--08773",
      "name": "string",
      "group": "string",
      "ip": "string",
      "port": 0,
      "ipc_enable": false,
      "ftp_enable": true,
      "status": "",
      "detail": ""
    }

  ]
}
  • 验证码:

我尝试从数据库-> API -> JSON ->计数“状态”
但我的只计算状态的总数,* 我想要不同的状态计数 *
例如:3“alert”,5“NOT_COMMUNICATING”

$(document).ready(function() {
    $.ajax({
      url: "http://192.168.10.01:3000/api/all_ipc/status",
      headers: {
        'Authorization': 'Bearer ' + getToken     // for getting Authorization
        
      },
      success: function(response) {
        var data = response.ipc_status;
        var statusCounts = {};

        for (var i = 0; i < data.length; i++) {
          var status = data[i].status;
          if (statusCounts[status]) {
            statusCounts[status]++;
          } else {
            statusCounts[status] = 1;
          }
        }

        console.log("Status Counts:", statusCounts);
      }
    });
  });
i5desfxk

i5desfxk1#

试过的代码是好的,我只是没有打印出来

$(document).ready(function() {
    $.ajax({
      url: "http://192.168.10.01:3000/api/all_ipc/status",
      headers: {
        'Authorization': 'Bearer ' + getToken     // for getting Authorization
        
      },
      success: function(response) {
        var data = response.ipc_status;
        var statusCounts = {};

        for (var i = 0; i < data.length; i++) {
          var status = data[i].status;
          if (statusCounts[status]) {
            statusCounts[status]++;
          } else {
            statusCounts[status] = 1;
          }
        }

        console.log("Status Counts:", statusCounts);
      }
    });
  });

相关问题