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