Javascript 将两个 fields/keys 相加

Javascript add two fields/keys together

这里是 Javascript 和一般编程方面的新手。如果您能提供任何帮助或指导,我将不胜感激。

我正在做一个副项目,从 rest API 中提取数据并将其显示在 html table 中。这是函数

function example(){

callApi('URL Here',function(apiObject){

   document.getElementById("result").innerHTML = "<pre>"+JSON.stringify(apiObject, null, 4)+"</pre>";
   });

};

function callApi(apiRequest,callback) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
        var jsonObject = JSON.parse(xhttp.responseText);
        callback(jsonObject);
      }
    };
    xhttp.open("GET", apiRequest, true);
    xhttp.send();
}

这是 JSON 输出的样子:

[
{
    "Start_Time_Stamp_UTC_ms": 1496121339796,
    "End_Time_Stamp_UTC_ms": 1496122179526,
    "End_Date": "Tue May 30 2017 15:29:39 GMT+1000 (GMT)",
    "Start_Date": "Tue May 30 2017 15:15:39 GMT+1000 (GMT)",
    "Meter": 350000562,
    "Protocol": "v4",
    "Count": 15,
    "rejected_bad": 0,
    "rejected_duplicates": 0,
    "kWh_Tot_Max": 2100.57,
    "Rev_kWh_Tot_Max": 1243.29
},
{
    "Start_Time_Stamp_UTC_ms": 1496120439546,
    "End_Time_Stamp_UTC_ms": 1496121279556,
    "End_Date": "Tue May 30 2017 15:14:39 GMT+1000 (GMT)",
    "Start_Date": "Tue May 30 2017 15:00:39 GMT+1000 (GMT)",
    "Meter": 350000562,
    "Protocol": "v4",
    "Count": 15,
    "rejected_bad": 0,
    "rejected_duplicates": 0,
    "kWh_Tot_Max": 2100.35,
    "Rev_kWh_Tot_Max": 1243.13
},
]

我需要帮助的是如何遍历输出并从 "kWh_Tot_Max" 值中减去 "Rev_kWh_Tot_Max" 值并将结果放入新的 "field/key"(不确定正确的术语)在数组中。

这是我要达到的目的:

[
{
    "Start_Time_Stamp_UTC_ms": 1496121339796,
    "End_Time_Stamp_UTC_ms": 1496122179526,
    "End_Date": "Tue May 30 2017 15:29:39 GMT+1000 (GMT)",
    "Start_Date": "Tue May 30 2017 15:15:39 GMT+1000 (GMT)",
    "Meter": 350000562,
    "Protocol": "v4",
    "Count": 15,
    "rejected_bad": 0,
    "rejected_duplicates": 0,
    "kWh_Tot_Max": 2100.57,
    "Rev_kWh_Tot_Max": 1243.29,
    "Net_kWh": 857.28
},
{
    "Start_Time_Stamp_UTC_ms": 1496120439546,
    "End_Time_Stamp_UTC_ms": 1496121279556,
    "End_Date": "Tue May 30 2017 15:14:39 GMT+1000 (GMT)",
    "Start_Date": "Tue May 30 2017 15:00:39 GMT+1000 (GMT)",
    "Meter": 350000562,
    "Protocol": "v4",
    "Count": 15,
    "rejected_bad": 0,
    "rejected_duplicates": 0,
    "kWh_Tot_Max": 2100.35,
    "Rev_kWh_Tot_Max": 1243.13,
    "Net_kWh": 857.22
},
]

如有任何帮助,我们将不胜感激!

您可以遍历 JSON array 并添加如下所示的属性

var json = [{
    "Start_Time_Stamp_UTC_ms": 1496121339796,
    "End_Time_Stamp_UTC_ms": 1496122179526,
    "End_Date": "Tue May 30 2017 15:29:39 GMT+1000 (GMT)",
    "Start_Date": "Tue May 30 2017 15:15:39 GMT+1000 (GMT)",
    "Meter": 350000562,
    "Protocol": "v4",
    "Count": 15,
    "rejected_bad": 0,
    "rejected_duplicates": 0,
    "kWh_Tot_Max": 2100.57,
    "Rev_kWh_Tot_Max": 1243.29,
    "Net_kWh": 857.28
  },
  {
    "Start_Time_Stamp_UTC_ms": 1496120439546,
    "End_Time_Stamp_UTC_ms": 1496121279556,
    "End_Date": "Tue May 30 2017 15:14:39 GMT+1000 (GMT)",
    "Start_Date": "Tue May 30 2017 15:00:39 GMT+1000 (GMT)",
    "Meter": 350000562,
    "Protocol": "v4",
    "Count": 15,
    "rejected_bad": 0,
    "rejected_duplicates": 0,
    "kWh_Tot_Max": 2100.35,
    "Rev_kWh_Tot_Max": 1243.13,
    "Net_kWh": 857.22
  },
];

for (var i = 0; i < json.length; i++) {
  json[i]["Net_kWh"] = json[i]["kWh_Tot_Max"] - json[i]["Rev_kWh_Tot_Max"];
}

console.log(json)

您可以使用 Array#map

var data = [{
    "Start_Time_Stamp_UTC_ms": 1496121339796,
    "End_Time_Stamp_UTC_ms": 1496122179526,
    "End_Date": "Tue May 30 2017 15:29:39 GMT+1000 (GMT)",
    "Start_Date": "Tue May 30 2017 15:15:39 GMT+1000 (GMT)",
    "Meter": 350000562,
    "Protocol": "v4",
    "Count": 15,
    "rejected_bad": 0,
    "rejected_duplicates": 0,
    "kWh_Tot_Max": 2100.57,
    "Rev_kWh_Tot_Max": 1243.29
  },
  {
    "Start_Time_Stamp_UTC_ms": 1496120439546,
    "End_Time_Stamp_UTC_ms": 1496121279556,
    "End_Date": "Tue May 30 2017 15:14:39 GMT+1000 (GMT)",
    "Start_Date": "Tue May 30 2017 15:00:39 GMT+1000 (GMT)",
    "Meter": 350000562,
    "Protocol": "v4",
    "Count": 15,
    "rejected_bad": 0,
    "rejected_duplicates": 0,
    "kWh_Tot_Max": 2100.35,
    "Rev_kWh_Tot_Max": 1243.13
  },
];

var output = data.map(function(d) {
  d.Net_kwh = d.kWh_Tot_Max - d.Rev_kWh_Tot_Max;
  return d;
});

console.log(output);

如果您也希望将其四舍五入到小数点后两位,请使用

d.Net_kwh = Number((d.kWh_Tot_Max - d.Rev_kWh_Tot_Max).toFixed(2));

这使用 toFixed() to reduce to 2 decimal places and Number() 将其转换回数字。