Given the current data structure, my goal is to calculate the average value for each column across all items in the contenders object. The next step is to convert this information into an array of arrays. Each subarray should consist of two values: the rounded average of the column and an incremental value starting from 0.
output = [[6, 0], [4, 1], [3, 2], [3, 3], [6, 4]];
Here's an illustration of the input structure:
input = {
categories: [
"Cat 1",
"Cat 2",
"Cat 3",
"Cat 4",
"Cat 5"
],
contenders: {
item1: [5, 3, 4, 4, 6],
item2: [6, 10, 4, 4, 6],
item3: [6, 3, 4, 9, 6],
item4: [8, 3, 5, 4, 6],
item5: [9, 3, 4, 4, 6],
item6: [10, 2, 7, 4, 6],
item7: [4, 3, 4, 4, 6],
item8: [1, 5, 4, 4, 6]
},
misc: [0, 3, 4, 4, 6]
};
I have a function that can handle calculating averages:
function getAvg(data) {
return data.reduce(function (p, c) {
return p + c;
}) / data.length;
}
However, I'm struggling with iterating through the item values to achieve the desired outcome.