I'm experimenting with creating a multidimensional array that looks like this:
var minutes = [];
for (let i = 1; i < 10; i++) {
minutes.push(i);
}
var works = [{
startTime: 80000,
endTime: 150000
}, {
startTime: 200000,
endTime: 400000
}, {
startTime: 15000,
endTime: 80000
}, {
startTime: 300000,
endTime: 500000
}];
var sumTime = 0;
var tasksByTime = [];
works.forEach((work, i) => {
tasksByTime.push(i);
console.log(tasksByTime);
var difference = work.endTime - work.startTime;
minutes.forEach((time, j) => {
console.log(time * 60 * 1000, difference);
console.log(i, j);
if (difference > time * 60 * 1000) {
tasksByTime[i][j] = (work.id);
}
});
sumTime += difference;
});
console.log(tasksByTime);
My goal is to arrange the objects based on the time difference between startTime
and endTime
. However, I encounter an issue when attempting to log the result:
TypeError: can't assign to property 0 on 3: not an object
Here's the complete log:
Array [ 0 ]
NumbersPanel.js:35
60000 8498 NumbersPanel.js:40
0 0 NumbersPanel.js:41
120000 8498 NumbersPanel.js:40
0 1 NumbersPanel.js:41
180000 8498 NumbersPanel.js:40
0 2 NumbersPanel.js:41
240000 8498 NumbersPanel.js:40
0 3 NumbersPanel.js:41
300000 8498 NumbersPanel.js:40
0 4 NumbersPanel.js:41
360000 8498 NumbersPanel.js:40
0 5 NumbersPanel.js:41
420000 8498 NumbersPanel.js:40
0 6 NumbersPanel.js:41
480000 8498 NumbersPanel.js:40
0 7 NumbersPanel.js:41
540000 8498 NumbersPanel.js:40
0 8 NumbersPanel.js:41
Array [ [], 1 ]
NumbersPanel.js:35
60000 12580 NumbersPanel.js:40
1 0 NumbersPanel.js:41
120000 12580 NumbersPanel.js:40
1 1 NumbersPanel.js:41
180000 12580 NumbersPanel.js:40
1 2 NumbersPanel.js:41
240000 12580 NumbersPanel.js:40
1 3 NumbersPanel.js:41
300000 12580 NumbersPanel.js:40
1 4 NumbersPanel.js:41
360000 12580 NumbersPanel.js:40
1 5 NumbersPanel.js:41
420000 12580 NumbersPanel.js:40
1 6 NumbersPanel.js:41
480000 12580 NumbersPanel.js:40
1 7 NumbersPanel.js:41
540000 12580 NumbersPanel.js:40
1 8 NumbersPanel.js:41
Array(3) [ [], 1, 2 ]
NumbersPanel.js:35
60000 12366 NumbersPanel.js:40
2 0 NumbersPanel.js:41
120000 12366 NumbersPanel.js
The issue persists until the 4th iteration. Any idea why this happens?
PS. Oddly enough, the code works in the snippet but crashes in the browser.
https://i.sstatic.net/Gvpdo.png
If it's relevant, I'm working with React. If React doesn't fit as a valid tag here, feel free to remove it.