I've got such an input object, which I should modify in order to use it with some charts library.
const input = {
foo: {
bar: 'biz',
},
statistic: {
"2019-11": {
A: 11,
B: 11,
C: 11,
D: 11,
E: 11,
},
"2019-12": {
A: 12,
B: 12,
C: 12,
D: 12,
E: 12,
},
....
}
};
My library expects an input like this:
[
{ subject: 'A', '2019-11': 11, '2019-12': 12 },
{ subject: 'B', '2019-11': 11, '2019-12': 12 },
{ subject: 'C', '2019-11': 11, '2019-12': 12 },
{ subject: 'D', '2019-11': 11, '2019-12': 12 },
{ subject: 'E', '2019-11': 11, '2019-12': 12 }
]
And this is my current solution:
const transform = arg => {
const keys = ['A', 'B', 'C', 'D', 'E'];
return _.map(keys, key => {
const data = _.reduce(arg.statistic, (result, value, k) => {
result[k] = value[key];
return result;
}, {});
return {
subject: key,
...data
}
});
}
Any improvements would be very welcome!