我有一個非常簡單的自制 API,它讀取服務(wù)器上一些文件的內(nèi)容,解析它們并以 JSON 發(fā)送它們的內(nèi)容。我的網(wǎng)頁使用 Ajax 調(diào)用 API,讀取數(shù)據(jù)并將它們存儲在自定義對象上。問題是,無論我在 JSON 中解析了多少文件,只有第一個文件被處理為我的 Javascript 代碼。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); } }});在console.log(data)for循環(huán)正確打印所有從API收到的DATAS,但之前console.log(data[i])只打印數(shù)組的第一個元素,我不明白為什么。
Javascript for循環(huán)在結(jié)束前停止
Qyouu
2021-06-02 18:50:52