Ignore map object Nodejs


I've got a recursive method for getting content of a directory, but I want to ignore certain object (e.g. if it's a .DS_store or something) but how can I get it to ignore it and carry on without returning a value.

Something like this but obviously continue doesnt work...

    info.children = fs.readdirSync(filename).map(function(child) {

        if (child.indexOf('.') == 0) 

        return dirTree(filename + '/' + child);


Problem courtesy of: Adam Maloney


Map itself performs 1:1 mapping, output array will contain the same number of elements as input array.

But you could chain filter and map methods:

info.children = fs.readdirSync(filename).filter(isValidFile).map(doStuff);

// Used to filter out unwanted entries (files)
function isValidFile (child) {
  return (child !== '.DS_store' && child.indexOf('.') !== 0);

// Used to perform your actual logic
function doStuff (child) {
  return dirTree(filename + '/' + child);
Solution courtesy of: kamituel


Use filter to first get only the right array elements and then map.

Something like:

info.children = fs.readdirSync(filename).filter(function (child) {
    return child.indexOf('.') !== 0;
}).map(function (child) {
    return dirTree(filename + '/' + child);
Discussion courtesy of: Paul Nechifor

This recipe can be found in it's original form on Stack Over Flow.