1

I have a files array that contains a set of text files in this format:

[test/fixtureData/fixtureData2/test3.inc, test/fixtureData/fixtureData3/test5.inc, test/fixtureData/test1.inc, ,test/fixtureData/test6.ssi ,test/fixtureData/test9.html]

I want to filter this array so that it filters out '.ssi' and '.html' files. And the code below:

var recursiveReadSync = require('recursive-readdir-sync'),
var fs = require('fs');

var foundFiles = [];
var files = fs.readdirSync(url);

I've recursed through a file and for simplicity of not pasting my whole code, assume the files array contains the items as listed above. I'm not sure how to filter this files array, i've looked at a few nodeJS api's and haven't found anything

2 Answers 2

2

Another solution without using regex, and using path module:

var path = require('path');
var filteredFiles = files.filter(function(value) {
    var ext = path.extname(value);
    return ['.inc', '.ssi'].indexOf(ext) == -1;
});

This is easier to maintain if you want the list of restricted extensions to be configurable.

Sign up to request clarification or add additional context in comments.

Comments

0

Array.filter is what you are looking for (if you only want the ssi/inc files, remove the not operator !):

files.filter(function(fileName) {
   return !fileName.match(/\.(inc|ssi)$/);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.