如何浏览 json 个文件 (JavaScript) 的目录?

How do I browse directories for json files (JavaScript)?

我想浏览 json 个文件的目录,并将所有 json 个文件合并到每个目录的一个 json 文件中。 我的 javascript 技能处于初级水平。

javascript 代码需要 运行 在终端中调用它,而不是在浏览器中。

您可以使用 glob npm 包来检索目录中的所有 json 文件路径。

全局:https://www.npmjs.com/package/glob

您可以简单地使用:

const glob = require("glob");
const fs = require('fs-extra');

async function processJsonData(){
  let parentDirectoryPath = '<specify path here>';
  let data = {};

  let jsonFilePaths = glob.sync(`${parentDirectoryPath}/*/*.json`);
  //this will return paths for all the json files 

    for (const jsonFilePath of jsonFilePaths) {
        let content = await fs.readJsonSync(jsonFilePath);
        Object.assign(data,content);
    }

     await fs.writeJson(<filePath>, data, {spaces: 2});
    //here specify your file path
}

ps。我是新贡献者,可能看错了问题。