Quote:
[...Array(61).keys()].map(i => 0)
That seems like a horribly inefficient method to produce a 61-element array of zeros. You create a new array, iterate over its keys, create a new array from that, then create a third array mapping each entry to zero.
You could simplify that to:
labels[data.statusCode] = Array(61).fill(0);
which only allocates a single array.
Quote:
(new Date(formattedResults.time).getTime() - 1670405100000) / 60000
Firstly, you're looking for a
time
property on the
formattedResults
array; you should be accessing the
data
variable instead.
Secondly, unless your
time
is always rounded to the nearest minute, that will return a floating-point number. If you want to use that as an index into your array, you need to round or truncate it.
Math.floor((new Date(data.time).getTime() - 1670405100000) / 60000)
You then have the same problem with the count - you're looking on
formattedResults
instead of
data
.
The fixed code:
let labels = {};
for (let index in formattedResults) {
let data = formattedResults[index];
if (!labels[data.statusCode]) {
labels[data.statusCode] = Array(61).fill(0);
}
let s = Math.floor((new Date(data.time).getTime() - 1670405100000) / 60000);
labels[data.statusCode][s] = data.count;
}
Demo[
^]