【发布时间】:2021-11-17 18:56:00
【问题描述】:
我需要有关 Js 中此代码的帮助。这个例子:
let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"];
let index = 0;
let counter = 0;
// // Output
// "1 => Sayed"
// "2 => Mahmoud"
while(index < friends.length){
index++;
if ( typeof friends[index] === "number"){
continue;
}
if (friends[index][counter] === "A"){
continue;
}
console.log(friends[index]);
}
当我这样做时,按摩出现在我面前,
seventh_lesson.js:203 Uncaught TypeError: Cannot read properties of undefined (reading '0') at Seventh_lesson.js:203.
然后我改变了这一行
if (friends[index][counter] === "A"){continue;
}
有了这个
if (friends[index].startsWith("A")){continue;}
还是不行,不知道为什么?
是数组中有数字的原因吗?
【问题讨论】:
标签: javascript arrays loops while-loop