tassadar
tassadar

Reputation: 3

importing multiple json files into a js array

I'm trying to having multiple json files imported to a js array

// module.js
module.exports={
    english:require("./englishfile"),
    chinese:require("./chineseFile"),
    french:require("./frenchFile"),
    spanish:require("./espFile"),
};
//js file
let allData=require("./module.js");

What this is doing is having all the files in a single array entry. I'm trying to have them as separate array entries for the entire size of module.js . I also would have a much larger number of files in module.js so I don't know its size and wouldn't be able to hard code them

Upvotes: 0

Views: 1147

Answers (1)

IVO GELOV
IVO GELOV

Reputation: 14259

You can do something like this

const requireModule = require.context('.',false,/\.json$/)
const modules = {}

requireModule.keys().forEach(filename => 
{
    const moduleName = fileName.replace(/(\.\/|\.json)/g, '');
    modules[moduleName] = requireModule(fileName)

    OR

    modules[moduleName] = 
    {
        namespaced: true,
        ...requireModule(fileName)
    }
});

export default modules;

Upvotes: 1

Related Questions