I'm having trouble calculating the average from my variable output. I keep getting NaN or zero as a result.
Here is my initial output:
console.log(data)
0: {nodeId: "53", SNR: "[38.2, 38, 37.9, 37.8, 37.6]", timestamp:
"2019-09-05 00:00:17"}
Next, I extract only the SNR values:
console.log(SNR);
["[38.2, 38, 37.9, 37.8, 37.6]"]
I attempted the code below to calculate the average, but it consistently returns 0 and doesn't recognize the numbers within the square brackets:
0[38.2, 38, 37.9, 37.8, 37.6]
$(document).ready(function() {
$.ajax({
url: "http://localhost:8080/data.php",
type: "GET",
success: function(data) {
console.log(data);
var SNR = [];
for (var i in data) {
SNR.push(data[i].SNR);
}
console.log(SNR);
const numbers = [SNR];
const sum = numbers.reduce((total, n) => total + n, 0);
console.log(sum);
},
error: function(data) {
}
});
});
Input information:
[{"nodeId":"53","SNR":"[38.2, 38, 37.9, 37.8, 37.6]","timestamp":"2019-09-05 00:00:17"},{"nodeId":"53","SNR":"[38.2, 37.9, 38.4, 37.9, 38.3]","timestamp":"2019-09-05 00:15:17"}]
Desired output after parsing SNR values:
0: 37.9
1: 38.1