如何在for循环中输出多维数组和单维数组

我想在控制台中输出以下代码,以便结果显示:


我周一的爱好是扑克、电子游戏


我周二的爱好是棋盘游戏、徒步旅行、攀岩


我周三的爱好是开车、购物


我知道这是相当基本的东西,但出于某种原因,我在网上找到的任何帮助都没有为我解决这个问题(当然我只是误读或误解了事情)。


我正在使用嵌套的 for 循环,我认为这是正确的,但由于某种原因,我无法通过多维爱好数组中的第一个数组。工作日按我的需要输出,但对于每个工作日,我想分别输出星期一、星期二和星期三变量 hobbies 中的第一个、第二个和第三个数组的全部内容。任何人都可以指出我正确的方向吗?


var hobbies = ['Poker','VideoGames'];


              ['Board Games', 'Hiking', 'Rockclimbing'];


              ['Driving', 'Shopping'];


const weekdays = ["Monday", "Tuesday", "Wednesday"];


for (i = 0; i < 3; i++) {

console.log("My hobbies on", weekdays[i], "are ");

for (x = 0; x < 5; x++)

  console.log(hobbies[?]);

}


肥皂起泡泡
浏览 114回答 3
3回答

呼唤远方

所以基本上你必须遍历数组并得到结果。var hobbies = [&nbsp; ['Poker', 'VideoGames'],&nbsp; ['Board Games', 'Hiking', 'Rockclimbing'],&nbsp; ['Driving', 'Shopping']];const weekdays = ["Monday", "Tuesday", "Wednesday"];for (i = 0; i < hobbies.length; i++) {&nbsp; console.log("My hobbies on", weekdays[i], "are "+ hobbies[i].join() );}更新了错误处理代码。function getWeekdayname(index){&nbsp; &nbsp; const weekdays = ["Monday", "Tuesday", "Wednesday"];&nbsp; &nbsp; if(index < weekdays.length){&nbsp; &nbsp; &nbsp; return weekdays[i];&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return 'X';}var hobbies = [&nbsp; &nbsp; &nbsp; ['Poker', 'VideoGames'],&nbsp; &nbsp; &nbsp; ['Board Games', 'Hiking', 'Rockclimbing'],&nbsp; &nbsp; &nbsp; ['Driving', 'Shopping'],&nbsp; &nbsp; &nbsp; ['Drivin 3g', 'Shoppin 6g']&nbsp; &nbsp; ];&nbsp; &nbsp; for (i = 0; i < hobbies.length; i++) {&nbsp; &nbsp; &nbsp; console.log("My hobbies on", getWeekdayname(i), "are "+ hobbies[i].join() );&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript