0

I'm trying to output objects of audio file names like this:

{
    "dog": ["sound1.mp3", "sound2.mp3"],
    "cat": ["catmeow.mp3", "play.mp3"]
}

Here is my code though it doesn't work:

const audioFolder = 'static/audio/';
import * as fs from 'fs';
import path from 'path';
let output = {};

const isNotImg = (e) => {
    const ext = path.extname(`${audioFolder}${e}`);
    return !(ext == '.jpg' || ext == '.png');
};

fs.readdir(audioFolder, (err, folders) => {
    folders.forEach((folder) => {
        if (isNotImg(folder)) {
            output.folder = [];
            console.log(folder, typeof folder);
            fs.readdir(`static/audio/${folder}`, (file) => {
                console.log(file);
                // file.forEach((e) => {
                //  output.folder.push(e);
                // });
            });
        }
    });
});

fs.readdir(`static/audio/dog/`, (files) => {
    console.log('1', files); //Output: 1 null
});

Here is the log:

1 null
cat string
dog string
null
null

Why doesn't this work?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
LocTran016
  • 25
  • 5
  • It should be `output[folder] = [];` instead of `output.folder = [];` and then, `output[folder].push(file)` – Titus Jun 07 '23 at 03:17

0 Answers0