I am currently facing an issue with an if statement inside a for loop.
The scenario involves retrieving a list of files from the directory filesSAS
, iterating through each one, and converting them from csv
to JSON
. After conversion, I check if the output contains an id in its object. If it does, I copy the file using the
copyFile(dirSas, dirOut, filename)
function. If an id is present, I add a date and save the file as CSV.
The problem arises during the first iteration where the file is copied, but the saveCSV
function is also executed, which overrides the desired result. My goal is to copy the file if the id is absent during this iteration and proceed to the next iteration. I have attempted to place the saveCSV
function inside the for loop with no success.
EDIT: In cases where the for loop encounters an object without an id, the file should be copied. If an id is present, I aim to append a date and save the file as CSV.
let noId = [{
user:"Mark",
job:"Job"
}]
let withId = [{
id:1,
user:"Mark",
job:"Job"
}]
output
let withId = [{
id:1,
user:"Mark",
job:"Job"
date: 12-09-2019
}]
const saveNewFile = async (filesSAS, dirSas, dirOut, dirArchive) => {
filesSAS.forEach(async filename => {
const newData = await csv().fromFile(`${dirSas.path}/${filename}`);
for await (const iterator of object) {
if (iterator.Id === null || iterator.Id === undefined) {
await copyFile(dirSas, dirOut, filename);
}
rec.Date = moment(Date.now()).format("DD-MMM-YYYY");
}
await saveCSV(newData, `${dirOut.path}/${filename}`, "output");
});
};
Best regards