根据其他字段值在json中添加一个字段

时间:2020-04-27 10:28:55

标签: javascript

我有这样的json,其中包含一些值:

     var returnData = [{
    "owner": "swapneil",
    "fleet_name": "RancorService",
    "creation_date": "09-03-2020",
    "environment": "RancorService/JP",
    "vip_port": 80,
    "partition": "YES",
    "protocol": "HTTP",
    "monitor": "Consistent (http-ping)",
    "lbset": "Consistent",
    "predictor": "Consistent",
    "spillover": "Consistent",
    "vip_cka": "Consistent :(NO)",
    "max_conns": "Consistent : (36)",
    "vip_cipher": null,
    "vip_cip": "Consistent",
    "vip_tier": "Consistent(Tier1)",
    "vip_siloed": "Consistent(Non-SILOED)",
    "vip_globalized": "Consistent(Non-GLOBALIZED)",
    "vip_dedicated": "Consistent(Non-DEDICATED_VIP)",
    "vip_retail_tag": "Consistent(RETAIL_TAG)",
    "down_stat_flush": "Consistent :(ENABLED)",
    "vip_type": "Consistent (BACKEND)",
    "server_port": "Consistent",
    "vip_server_timeout": "Consistent",
    "vip_client_timeout": "Consistent",
    "persistent_method": "Consistent",
    "persistent_timeout": "Consistent"

  },
  {
    "owner": "swapneil",
    "fleet_name": "RancorService",
    "creation_date": "09-03-2020",
    "environment": "RancorService/JP",
    "vip_port": 443,
    "partition": "YES",
    "protocol": "TCP",
    "monitor": "NC",
    "lbset": "NC (>1 Vip's in same LB)",
    "predictor": "NC (leastconns,ROUNDROBINSHOULD BE ROUNDROBIN)",
    "spillover": "Consistent",
    "vip_cka": "Consistent :(NO)",
    "max_conns": "Consistent : (9000)",
    "vip_cipher": null,
    "vip_cip": "Consistent",
    "vip_tier": "Consistent(Non-Tier1)",
    "vip_siloed": "Consistent(Non-SILOED)",
    "vip_globalized": "Consistent(Non-GLOBALIZED)",
    "vip_dedicated": "Consistent(Non-DEDICATED_VIP)",
    "vip_retail_tag": "Consistent(RETAIL_TAG)",
    "down_stat_flush": "Consistent :(ENABLED)",
    "vip_type": "Consistent (BACKEND)",
    "server_port": "Consistent",
    "vip_server_timeout": "Consistent",
    "vip_client_timeout": "Consistent",
    "persistent_method": "Consistent",
    "persistent_timeout": "Consistent"

  },
  {
    "owner": "swapneil",
    "fleet_name": "RegionAwareBaseLayer",
    "creation_date": "09-03-2020",
    "environment": "RegionAwareBaseLayer/NA",
    "vip_port": 80,
    "partition": "YES",
    "protocol": "HTTP",
    "monitor": "Consistent (http-ping)",
    "lbset": "Consistent",
    "predictor": "Consistent",
    "spillover": "Consistent",
    "vip_cka": "Consistent :(NO)",
    "max_conns": "Consistent : (56)",
    "vip_cipher": null,
    "vip_cip": "Consistent",
    "vip_tier": "NC : >1 VIP's MISSING TIER-1 TAG",
    "vip_siloed": "Consistent(Non-SILOED)",
    "vip_globalized": "Consistent(GLOBALIZED)",
    "vip_dedicated": "Consistent(Non-DEDICATED_VIP)",
    "vip_retail_tag": "Consistent(RETAIL_TAG)",
    "down_stat_flush": "Consistent :(ENABLED)",
    "vip_type": "Consistent (BACKEND)",
    "server_port": "Consistent",
    "vip_server_timeout": "Consistent",
    "vip_client_timeout": "Consistent",
    "persistent_method": "Consistent",
    "persistent_timeout": "Consistent"

  },
  {
    "owner": "swapneil",
    "fleet_name": "RegionAwareBaseLayer",
    "creation_date": "09-03-2020",
    "environment": "RegionAwareBaseLayer/NA",
    "vip_port": 443,
    "partition": "NO",
    "protocol": "TCP",
    "monitor": "Consistent (ssl-ping)",
    "lbset": "Consistent",
    "predictor": "NC :SHOULD BE ROUNDROBIN",
    "spillover": "Consistent",
    "vip_cka": "NC : SHOULD BE DISABLED (YES)",
    "max_conns": "Consistent : (9000)",
    "vip_cipher": null,
    "vip_cip": "Consistent",
    "vip_tier": "Consistent(Tier1)",
    "vip_siloed": "Consistent(Non-SILOED)",
    "vip_globalized": "Consistent(Non-GLOBALIZED)",
    "vip_dedicated": "Consistent(Non-DEDICATED_VIP)",
    "vip_retail_tag": "Consistent(RETAIL_TAG)",
    "down_stat_flush": "Consistent :(ENABLED)",
    "vip_type": "Consistent (BACKEND)",
    "server_port": "Consistent",
    "vip_server_timeout": "Consistent",
    "vip_client_timeout": "Consistent",
    "persistent_method": "Consistent",
    "persistent_timeout": "Consistent"

  },
  {
    "owner": "swapneil",
    "fleet_name": "RegionMappingService",
    "creation_date": "09-03-2020",
    "environment": "RegionMappingService/EU",
    "vip_port": 80,
    "partition": "YES",
    "protocol": "HTTP",
    "monitor": "Consistent (http-ping)",
    "lbset": "Consistent",
    "predictor": "Consistent",
    "spillover": "Consistent",
    "vip_cka": "Consistent :(NO)",
    "max_conns": "Consistent : (16)",
    "vip_cipher": null,
    "vip_cip": "Consistent",
    "vip_tier": "Consistent(Tier1)",
    "vip_siloed": "Consistent(Non-SILOED)",
    "vip_globalized": "Consistent(Non-GLOBALIZED)",
    "vip_dedicated": "Consistent(Non-DEDICATED_VIP)",
    "vip_retail_tag": "Consistent(RETAIL_TAG)",
    "down_stat_flush": "Consistent :(ENABLED)",
    "vip_type": "Consistent (BACKEND)",
    "server_port": "Consistent",
    "vip_server_timeout": "Consistent",
    "vip_client_timeout": "Consistent",
    "persistent_method": "Consistent",
    "persistent_timeout": "Consistent"

  },
  {
    "owner": "swapneil",
    "fleet_name": "RegionMappingService",
    "creation_date": "09-03-2020",
    "environment": "RegionMappingService/JP",
    "vip_port": 443,
    "partition": "YES",
    "protocol": "TCP",
    "monitor": "NC",
    "lbset": "NC (>1 Vip's in same LB)",
    "predictor": "NC :SHOULD BE ROUNDROBIN",
    "spillover": "Consistent",
    "vip_cka": "Consistent :(NO)",
    "max_conns": "Consistent : (9000)",
    "vip_cipher": null,
    "vip_cip": "Consistent",
    "vip_tier": "Consistent(Non-Tier1)",
    "vip_siloed": "Consistent(Non-SILOED)",
    "vip_globalized": "Consistent(Non-GLOBALIZED)",
    "vip_dedicated": "Consistent(Non-DEDICATED_VIP)",
    "vip_retail_tag": "Consistent(RETAIL_TAG)",
    "down_stat_flush": "Consistent :(ENABLED)",
    "vip_type": "Consistent (BACKEND)",
    "server_port": "Consistent",
    "vip_server_timeout": "Consistent",
    "vip_client_timeout": "Consistent",
    "persistent_method": "Consistent",
    "persistent_timeout": "Consistent"

  }
];

我想在每个数据块中添加另一个字段“审计分数”。 审核分数的值取决于其他字段的值,这样,每次以“ Consistent”开头的字段的值时,审核分数就会增加5。

我为此编写了一个函数:

    function addAuditScore(returnData)
    {
        var col = [];
        for (var i = 0; i < returnData; i++) {
          for (var key in returnData[i]) {
            if (col.indexOf(key) === -1) {
              col.push(key);
            }
          }
        }
        for (var i = 0; i < returnData; i++)
        {
            var score = 0;
            for (var j = 0; j < col.length; j++)
            {
               var tempVal = (returnData[i][col[j]] != null) ? returnData[i][col[j]].toString() : "";
               if (tempVal.startsWith("Consistent"))
               {
                score+= 5; 
               }
            }
            returnData[i]['Audit Score'] = score;
        }
    }

但是它不起作用。谁能帮忙?

0 个答案:

没有答案