title: filter extension while checking folder path using node js
body: <p>I have a code which check all the directories and sub-directories so while checking the directories and sub directories i want to check if the specific extension file is present or not if present then i want to filter them in two array</p>
  <p>code i have to check all the files is</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>async function checkFileLoc(folderPath, depth) {
    depth -= 1;
    let files = await fsPromises.readdir(folderPath);
    files = await Promise.all(
      files.map(async (file) =&gt; {
        const filePath = path.join(folderPath, file);
        const stats = await fsPromises.stat(filePath);
        if (stats.isDirectory() &amp;&amp; depth &gt; 0) {
          return checkFileLoc(filePath, depth);
        } else if (stats.isFile()) return filePath;
        else return null;
      })
    );
    return files
      .reduce((all, folderContents) =&gt; all.concat(folderContents), [])
      .filter((e) =&gt; e != null);
  }</code></pre>
  </div>
  </div>
  </p>
  <p>Code to check if the <code>.jpg</code> ext file present or not but its not working for me and i want to filter further.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>async function FilterFile(folderPath) {
    wantExt = [".jpg"];
    let  parts;
    const paths = await checkFileLoc(folderPath, 3);
  
    const otherFiles = [];
    
    for (const filePath of paths) {
      parts = filePath.split("/");
      let splitFileName = parts[parts.length - 1].split(".");
      if (wantExt.includes(`.${splitFileName[splitFileName.length - 1]}`)) {
        otherFiles.push(filePath);
      }
    }
    return { otherFiles };
  }</code></pre>
  </div>
  </div>
  </p>
  <p>I want to check if my location contain all the <code>.jpg</code> file or not if it contain then its should filter <code>_bio.jpg</code> and other <code>.jpg</code> files differently.</p>
  <pre><code>Output:
  bioArr=
  [ &quot;animal_bio.jpg&quot;
    &quot;mammal_bio.jpg&quot;
  ]
  otherArr=
  [ &quot;tree_doc.jpg&quot;
    &quot;human.jpg&quot;
    &quot;flowes_info.jpg&quot;
  ]
  </code></pre>