Check out the structure of my JSON File below.
{
"questions": ["Question1", "Question2"],
"orgs": ["Org1", "Org2", "Org3"],
"dates": ["Q1", "Q2", "Q3"],
"values": [
[
[5, 88, 18],
[50, 83, 10],
[29, 78, 80]
],
[
[46, 51, 61],
[95, 21, 15],
[49, 86, 43]
]
]
}
I am attempting to extract a single array of values by looping through each question, indexed using an "orgs" value and then adding each retrieved value while dividing it by data.dates.length.
Below is the code I have;
d3.json("data.json", function(error, data) {
var array = new Array()
var orgS = "Org2"
var org = data.orgs.indexOf(orgS);
for (var question = 0; question < data.questions.length; question++) {
array.push(
data.values[question][org]
)
console.log(array)
}
// add array together
array.reduce(function(a, b) {
return a + b;
})
// calculate average
var avg = array / data.dates.length;
})
Here is a plnk;
http://plnkr.co/edit/wMv8GmkD1ynjo9WZVlMb?p=preview
The issue I believe lies in how I am retrieving the values initially? Currently, even though the correct values are being displayed in the console log, they are showing up twice and both times inside nested arrays. Not sure how to fix this problem?
Just as a guide;
[question1][org1] corresponds to the values [5, 88, 18].
Any helpful advice would be much appreciated!
Thank you!