Hi !
I have a basic question how to parse json which I get from the HTTP Binding.
My JSON looks like the following:
{
“getusertransactions”:{
“version”:“1.0.0”,
“runtime”:11.106967926025,
“data”:{
“transactions”:[
{
“id”:6975736,
“username”:“arnoldmaderthaner”,
“type”:“Fee”,
“amount”:0.0211522,
“coin_address”:null,
“timestamp”:“2018-01-10 13:19:27”,
“txid”:null,
“height”:1789426,
“blockhash”:“af0c85ad9ec7753a426128d3e77384d2bc60925d420fc07cf0885cd8e8a12e32”,
“confirmations”:25
},
{
“id”:6975717,
“username”:“arnoldmaderthaner”,
“type”:“Credit”,
“amount”:2.11522006,
“coin_address”:null,
“timestamp”:“2018-01-10 13:19:27”,
“txid”:null,
“height”:1789426,
“blockhash”:“af0c85ad9ec7753a426128d3e77384d2bc60925d420fc07cf0885cd8e8a12e32”,
“confirmations”:25
},
{
“id”:6970074,
“username”:“arnoldmaderthaner”,
“type”:“Fee”,
“amount”:0.02569984,
“coin_address”:null,
“timestamp”:“2018-01-10 13:16:29”,
“txid”:null,
“height”:1789422,
“blockhash”:“d1225b61a20a14ca352644e775db83beb5f66549f29394a3cb2e387260daaf05”,
“confirmations”:29
}
],
“transactionsummary”:{
“Credit”:3797.41269803,
“Debit_AP”:3517.01453348,
“Fee”:37.97412718,
“TXFee”:0.07
}
}
}
}
Now I want to write some javascript to get the average amount of all the transactions that have a type equal to “Credit”.
Here is my javascript:
(function(i) {
var data = JSON.parse(i).getusertransactions.data.transactions;
var sum = 0, n = 0;
for (j in data) {
if(j.type == “Credit”)
{
sum += j.amount;
n++;
}
}
console.log(sum/n);
return sum/n;
})(input)
anyone knows what could be wrong with the JS ?