I wrote a program that's supposed to output all the names of files having a defined extension in a defined folder, however when I add the following line:
list = list.filter(file => { file !== fileExtension });
Which I want to filter the files which name matches an extension name (A file called "txt" for example), I get not output.
Here is the complete code (I use node JavaScript to run it):
const fs = require('fs');
const dirPath = process.argv[2];
const fileExtension = process.argv[3];
fs.readdir(dirPath, (err, list) => {
if (err) {
return console.log('An error occurred while reading directory: ' + err);
}
list = list.filter(file => { file !== fileExtension }); // No output when I add this line
list = list.filter(file => file.split('.')[file.split('.').length - 1] === fileExtension);
list.forEach((file) => {
console.log(file);
});
});