是否有任何等價的metod使得像php mysql_fetch_array while js循環?while(var row = fn());
在PHP你可以這樣做:
<?php
while ($row = mysql_fetch_array($result)) {
echo "my name is " . $row['name'] . " and i'm " . $row['age'] . " yeas old";
}
?>
我有這樣的對象/數組:
function fetch_array(arr) {
// My magic fn that dose not work well, b/c it change the orginal refence
return arr.shift();
}
var result = [{
name: "Bob",
age: 12
}, {
name: "Jim",
age: 18
}]
// And want to do:
while (row = fetch_array(result)) {
alert("my name is " + row["name"] + " And I'm " + row['age'] + " years old");
}
// returns zero :( dont want that
console.log(result.length);
您的代碼完美的作品... – 2012-03-30 14:42:04
做,我認爲他們想要的陣列將保持不變。 – geekchic 2012-03-30 14:42:34
記得聲明變量'row',否則它將是一個隱式的全局變量。 – Zirak 2012-03-30 15:10:22