2017-05-30 58 views
1

非常多的新手在這裏與JavaScript和在編程一般。我會感謝您提供的任何幫助或指導。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 
}, 
] 

任何幫助將不勝感激!

回答

1

您可以使用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);

如果你正在尋找把它關掉到小數點後2位,以及使用

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

這使用toFixed()減少到小數點後2位,並Number()將其轉換回數字。

+0

這是迄今爲止最好的答案,map比JavaScript中的for循環更具慣用性,並且儘可能地避免突變是很好的。 –

+0

謝謝你們!很好的幫助! – campbellpng

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)