0
在下面的代码中,为什么我被当作字符串对待?我必须将其倍数为1才能将其转换回数字。为什么javascript将字符串视为变量?
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;
}
尝试1 +我,而不是我+ 1 – galvan
@galvan操作数的顺序不会改变结果。如果其中任何一个是字符串,'+'会执行连接。 –
为什么不'Array.indexOf'? – IMTheNachoMan