Javascript将两个字段/键添加到一起

时间:2017-05-30 05:55:42

标签: javascript arrays calculation

这里有很多新手,使用Javascript和编程。我很感激您提供的任何帮助或指导。

我正在开展一个侧面项目,从其他API中提取数据并将其显示在html表中。这是函数

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”值,并将结果放入数组中的新“字段/键”(不确定正确的术语)

以下是我想要的内容:

[
{
    "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
},
]

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

您可以迭代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)

答案 1 :(得分:1)

您可以使用Array#map

&#13;
&#13;
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);
&#13;
&#13;
&#13;

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

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

这会使用toFixed()减少到2位小数,Number()将其转换回数字。