我在JavaScript中使用了for-in循环,但我只对键感兴趣。
for(var key in { foo:0, bar:0, blah:0 }) {
/* do sth. with the key */
}
这个方法可以运行,但是看起来有点愚蠢。Firefox提供了一个for-of循环,但不幸的是它并不能在所有浏览器中运行。我也在Opera 11中测试过它,但是它在那里也不能工作。
// only firefox
for(var key of ["foo", "bar", "blah"]) {
/* do sth. with the key */
}
有没有更智能的方法来解决这个问题,适用于所有浏览器?