You could filter they array of files with an extension extractor function. The path
module provides one such function, if you don't want to write your own string manipulation logic or regex.
const path = require('path');
const EXTENSION = '.txt';
const targetFiles = files.filter(file => {
return path.extname(file).toLowerCase() === EXTENSION;
});
EDIT
As per @arboreal84's suggestion, you may want to consider cases such as myfile.TXT
, not too uncommon. I just tested it myself and path.extname
does not do lowercasing for you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…