我制作了一個 for 回圈,以便控制臺記錄陣列中的多個條目。然而,for 回圈只回傳陣列中的最后一個條目,而不是從 0 到陣列末尾的所有內容。
for (var i = 0; i < roa.length; i ) {questionContentRoa = roa[i].questionContent, correctAnswerRoa = roa[i].correctAnswer } console.log(questionContentRoa, correctAnswerRoa);
uj5u.com熱心網友回復:
如果您稍微識別一下代碼,您會更清楚。
超出范圍,因此console.log,它只記錄回圈結束之前的最后一個分配。
for (var i = 0; i < roa.length; i ) {
questionContentRoa = roa[i].questionContent;
correctAnswerRoa = roa[i].correctAnswer;
}
console.log(questionContentRoa, correctAnswerRoa);
uj5u.com熱心網友回復:
console.log 應該在 for 回圈內。你已經把它放在外面了。
或者,更簡單的遍歷陣列的方法是使用forEach回圈。
roa.forEach((item)=>{
console.log(item.questionContent, item.correctAnswer );
});
// in the first iteration item would be the roa[0],
//then in the second iteration it would be roa[1] and so on until the array ends..
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495152.html
標籤:javascript 数组 for循环
