I got a FileHelper-method that lists the name of all files and eventually returns the filenames:
import fs from 'fs';
import path from 'path';
class FileHelper {
static ListFiles() {
const fileDir = `${path.resolve()}/Specifications/`;
let files = fs.readdirSync(fileDir);
for (const file in files) {
console.log(file); // Prints filename correctly: 'petstore.json'
}
return files;
}
}
export default FileHelper;
However, when I invoke this method and print it once again in a for-loop it prints the array index and not the value:
import FileHelper from './Helpers/FileHelper.js';
function main() {
try {
let specifications = FileHelper.ListFiles();
for (const specification in specifications) {
console.log(specification); // prints '0' instead of 'petstore.json'
}
}
catch(err) {
console.error(err);
}
}
main();
Why does it not print the file name in the second for-loop? Thanks!