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