我正在嘗試myCourses使用推送方法創建類似于陣列的新陣列 。
但在某種程度上,它一次只記錄一個字串,而不是創建一個類似陣列的新myCourses陣列:
let myCourses = ["Learn CSS Animations", "UI Design Fundamentals", "Intro to Clean Code"]
for (let i = 0; i < myCourses.length; i ) {
let a = []
a.push( a = myCourses[i] )
console.log(a)
}
uj5u.com熱心網友回復:
由于我上面的評論,正確的解決方案(如果我們接受以這種方式創建新陣列,使用 for 回圈)是
let myCourses = ["Learn CSS Animations", "UI Design Fundamentals", "Intro to Clean Code"]
// declare a only once
let a = []
for (let i = 0; i < myCourses.length; i ) {
// a = myCourses[i] is non-sense in this case
a.push( myCourses[i] )
}
console.log(a);
// write into console the result, just once, not in every loop
// returns ["Learn CSS Animations", "UI Design Fundamentals", "Intro to Clean Code"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/382268.html
標籤:javascript 数组 推
上一篇:在Java中回傳byte[]而不是BufferedImage?
下一篇:Numpy錯誤識別資料型別
