I have a really simple home made API that reads the content of some files on the server, parse them and send their content in JSON.
My webpage calls the API using Ajax, reads the data and store them on custom objects. The problem is that, no matter of much files I have parsed in my JSON, only the first one is treated my the Javascript code.
runs = [];
function Solution(values) {
this.number = values[0]
this.weight = values[1]
this.value = values[2]
this.score = values[3]
}
function DateValue(date) {
regex = new RegExp(/(\d{4})-(\d{1,2})-(\d{1,2})-(\d{1,2}):(\d{1,2}):(\d{1,2})-(\d{1,2})/)
dateArray = date.split(regex)
this.year = dateArray[1]
this.month = dateArray[2]
this.day = dateArray[3]
this.hour = dateArray[4]
this.minutes = dateArray[5]
this.secondes = dateArray[6]
}
function Run(values) {
this.algorithm = values["log"]["Algorithm used"]
this.weight = values["log"]["Weight"]
this.value = values["log"]["Value"]
this.date = new DateValue(values["log"]["Date"])
this.solutions = []
for(i = 0; i < values["datas"].length; i++) {
this.solutions.push(new Solution(values["datas"][i]))
}
}
$.ajax({
url: 'api.php', // the data sent by the API is a valid JSON
dataType: 'json',
success: function(data, status) {
console.log(data);
for(i = 0; i < data.length; i++) {
console.log(data[i]);
var run = new Run(data[i])
runs.push(run);
}
}
});
The console.log(data)
before the for loop prints correctly all the datas recieved from the API, but the console.log(data[i])
prints only the first element of the array, and I can't understand why.