在下面的代码中为什么将 i 视为字符串?我必须将其乘以 1 才能将其转换回数字。
getPositionInArray(value, array) {
console.log('array = ', array);
let i = 0; // why is i a string?
for (i in array) {
if (array[i].toLowerCase() === value) {
let positionOnUI = i * 1 + 1; // why can't I use i + 1?
return positionOnUI;
}
}
return null;
}
最佳答案
只要使用普通的 for 循环就不会出现此问题:
function getPositionInArray (value, array) {
console.log('array = ', array);
for (let i = 0; i < array.length; i++) {
if (array[i].toLowerCase() === value) {
let positionOnUI = i // why can't I use i + 1?
return positionOnUI;
}
}
return null;
}
关于javascript - 为什么javascript将变量视为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37285250/