If you’re in control of the original data, then it’s all good, but if not you would want to evaluate the key name in your condition instead of relying on it always being in the same positional order.
One of the smart kids will have to chime in with something more elegant!
With the series key, I assume this is for charting. We have used RunJS to get the data in the format we want.
First we used custom query to write a query that could return as close of a structure as possible, and then use RunJS to get the final JSON data.
I don’t have an exact code to share with you, but nested loops should help you get the data structure you want.