Is there a way to load an unknown number of .json files into an array using JavaScript?
For example:
(in: .../example-folder)
abc.json
xyz.json
uvw.json
array.length == 3
(in .../example_folder)
abc.json
xyz.json
array.length == 2
If you are unsure about the total number of files, how can this be achieved efficiently in JavaScript?
Thank you for your help!
Best regards,
Soxxes
Edit: Typically, I approach this task like so:
function loadJSON(url, callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType('application/json');
xobj.open('GET', url, true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == '200') {
callback(xobj.responseText);
}
};
xobj.send(null);
}
loadJSON("../example_folder/abc.json", function(res){
data_parsed = JSON.parse(res);
data_stringified = JSON.stringify(data_parsed, null, 4);
abc = data_stringified;
});