I have a list of JSON files in a variable and I want to load the content of these files into a single object. The json files have two keys: metadata and outputs.
Once this is done, I want to call a function that generates a list of tables. I am able to do this if I have only one file. The code I use to do so is:
jQuery.getJSON(filePath, function(data) {
jQuery.each(data, function(key, val){
if ( key === "outputs"){
new tableGenerator(val);
};
});
});
when I try to get the data from different files I obtain an empty variable. To load different files I use:
var fileList = ["dataFolder/data1.json",
"dataFolder/data2.json",
"dataFolder/data3.json"]
var jsonData = [];
jQuery.when(
fileList.forEach( file => {
jQuery.getJSON(file, function(data) {
jQuery.each(data, function(key, val){
if ( key === "outputs"){
jsonData = jsonData.concat(val);
};
});
});
})
).then(function(){
console.log(jsonData);
new tableGenerator(jsonData);
})
I don't work normally in javascript and I don't understand why normally the tableGenerator function is executed before the jsonData handler is filled.
Any comment in the code is welcome (style, deprecated...) as I am not a javascript developer and probably a lot of things will be done in an uncommon way.