如果两个数组包含相同数量的项目,那么您可以使用普通的 for 循环来访问具有相同索引的每个项目,如下所示 -
let home_team = ["A", "B", "C"];
let away_team = ["D", "E", "F"];
for(let i=0; i<home_team.length; i++){
console.log(`${home_team[i]} VS ${away_team[i]}`);
}
展开片段
或者,您也可以在 for 循环中使用多个计数器,如下所示(尽管您现在不需要这个 -
let home_team = ["A", "B", "C"];
let away_team = ["D", "E", "F"];
for(let i=0,j=2; i<home_team.length; i++, j--){
console.log(`${home_team[i]} VS ${away_team[j]}`);
}
慕婉清6462132
相关分类