0

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

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.