I have a set of files a.json, b.json, c.json, etc., and I want to load the data contained therein to a single data structure in JavaScript. However, the last line of the following code always returns 0 instead of 3 which I expect. What am I doing wrong?
files = ["a", "b", "c"];
var dataset = [];
function getObject(file) {
return $.getJSON(file).then(function (data) {
return data;
});
}
for (i = 0; i < files.length; i++) {
getObject(files[i] + ".json").done(function (data) {
dataset.push(data);
});
}
alert(dataset.length)