-1
我有這個功能,我想重新排列數組中最後找到的所有0,我正在使用一個臨時數組。一切都很好,直到我檢查了testArray
這是作爲參數收到的原始數組的長度。爲什麼我找不到他的長度以及第一個for
如果他的長度未定義如何工作?無法獲得作爲參數收到的數組的長度
window.onload = function() {
var testArray =[5,3,0,55,0,9,0,8];
function moveArray(testArray){
var tempArray= [];
console.log(tempArray.length + " vs " + testArray.lenght);
for(var i = 0 ;i < testArray.length; i++) {
if(testArray[i] != 0){
tempArray.push(testArray[i]);
// console.log("pushed " + testArray[i]);
}
}
// console.log(tempArray.length + " vs " + testArray.lenght);
while(tempArray.length < testArray.lenght){
tempArray.push('0');
// console.log('push 0');
}
testArray=tempArray;
for(var i = 0 ;i < testArray.length; i++){
// console.log(testArray[i]);
}
};
moveArray(testArray);
};
你只是想移動的0到最後還是你也想的其餘值排序? – charsi
'lenght' **長度** –
testArray.lenght?它應該是testArray.length – Oxi