【发布时间】:2021-08-26 06:04:10
【问题描述】:
这是对象:
const game = {
team1: 'Bayern Munich',
team2: 'Borrussia Dortmund',
players: [
[
'Neuer',
'Pavard',
],
[
'Burki',
'Schulz',
],
],
score: '4:0',
scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'],
date: 'Nov 9th, 2037',
odds: {
team1: 1.33,
x: 3.25,
team2: 6.5,
},
};
我想使用for..of 并在循环时跟踪数组索引。
可能的解决方案:
for (const [i, player] of game.scored.entries())
console.log(`Goal ${i + 1}: ${player}`);
或者:
let i = 0
for(const game1 of game.scored) {
i++;
console.log(i,game1)
}
但是是否可以在 for 语句中声明任何变量并对其进行跟踪?
谢谢。
【问题讨论】:
-
为什么不采取标准
for?
标签: javascript arrays for-loop