I have an array with 101 values (representing people aged 0-100).
What would be a preferably fast and simple way of building these aggregated arrays in one go:
var input = [55,33,12 .. 98 more]
var output = {
//same as input
i1 = [],
//0-5, 6-10, 11-15 ... 96-100
i5 = [],
//0-10, 11-20, 21-30 ... 91-100
i10 = [],
//0-20, 21-40, 41-60 ... 81-100
i20 = [],
}
On a side note: Would you name these aggregate arrays by the interval ("i1", "i5") or by the number of groups/elements ("g100", "g20") - what is more intuitive if another programmers comes across these definitions?
You can reuse results of the aggregation to calculate the next array.
Note that, as xanatos said, performance is not really of big concern here.
PS1: was not sure if you were trying to make output an object (as in this code) or 2D array.
PS2: since your first group always has 1 more element, you might need to adjust the code a bit for this special case.