Programming:
const fs = require("fs");
const { parse } = require("csv-parse");
function fetchSchedule() {
let scheduleData = [];
fs.createReadStream("./timetable.csv")
.pipe(parse({ delimiter: ",", from_line: 2 }))
.on("data", function (row) {
let classCode = "ΘΗΨ3β";
// Extracting relevant items for classes and storing them in a variable
for (let i = 0; i < row.length; i++) {
if (row[i].includes(classCode) || row[i].includes("ΓΘ2Φ5-3") || row[i].includes("ΓΘ1Μ7-3")) {
scheduleData.push({ number: i, text: row[i] });
}
}
// Sorting the extracted items
for (let i = 0; i < scheduleData.length; i++) {
if (scheduleData[i + 1]) {
if (scheduleData[i].number > scheduleData[i + 1].number) {
let temp = scheduleData[i];
scheduleData[i] = scheduleData[i + 1];
scheduleData[i + 1] = temp;
}
}
}
let numberOfObjects = 8 // <-- determines number of objects in each group
// Grouping items into sets of 8
let groupedItems = scheduleData.reduce((acc, elem, index) => {
let rowNum = Math.floor(index / numberOfObjects) + 1;
acc[`${rowNum}`] = acc[`${rowNum}`] || [];
acc[`${rowNum}`].push(elem);
return acc
}, {});
console.log(groupedItems)
});
}
fetchSchedule();
Desired Output Pattern:
{
'1': [
{ number: 3, text: 'ΓΘ1Μ7-3 / 16 / Μαθηματικά...' },
{ number: 4, text: 'ΘΗΨ3β / 24 / Ψηφ.Ηλεκτρονικά II Γ ΘΗΥ' },
...
],
.....
'5': [
{ number: 37, text: 'ΓΘ1Μ7-3 / 16 / Μαθηματικά...' },
...
]
}
The current issue is that there are consistently 8 elements in the first and fourth sets, while the second, third, and fifth have 7 items. I've attempted various solutions including conditional statements and adding empty elements between the last and first of each set but haven't succeeded.