A = { 1:1, 2:2, 3:3, 4:4, 5:5 };
for (var i in A) {
console.log(i);
if (A[i] == 4) delete A[i], A[7] = 7;
if (A[i] == 2) delete A[i], A[0] = 0;
if (A[i] == 7) console.log('seven');
if (A[i] == 0) console.log('zero');
}
console.log(A);
這似乎工作,但我不知道執行for(.. in ..)循環的細節,以確保它在大多數情況下是安全的。修改(..in ..)循環中的對象是否安全?
See also here the same question about arrays.
http://es5.github.io/#x12.6.4 – Teemu