JS遗漏点

167 阅读1分钟

for...of遍历获取键和值

  • 使用Array.prototype.keys:
for (const index of [1, 2, 3, 4, 5].keys()) {
  console.log(index);
}
  • 如果要同时访问键和值,可以使用Array.prototype.entries():
for (const [index, value] of [1, 2, 3, 4, 5].entries()) {
  console.log(index, value);
}