how can I able to find the average of the polarity data from the JSON file? I just can't seem to access it. I need the average as a var so I can use this input to my chart, using "chart.js" API.
Here is my code below:
var sum = 0;
var avg = 0;
var xmlhttp = new XMLHttpRequest();
var url = "moderna_updated.json";
xmlhttp.open("GET",url,true);
xmlhttp.send();
xmlhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
var data = JSON.parse(this.responseText);
var polarities = data.tweets.map(function(elem){
return elem.polarity;
});
//console.log(polarities);
}
}
Here is a sample of my JSON file below:
{
"tweets": [
{
"tweetcreatedts": "2021-03-10 23:59:51",
"polarity": 0.0,
"score": "neutral"
},
{
"tweetcreatedts": "2021-03-10 23:59:51",
"polarity": 0.15,
"score": "positive"
},
{
"tweetcreatedts": "2021-03-10 23:59:36",
"polarity": 0.0,
"score": "neutral"
},
{
"tweetcreatedts": "2021-03-10 23:59:36",
"polarity": 0.4,
"score": "positive"
]}