先来 for in 形式遍历数组
<pre><code>
array = ['tt', 'dl', 'king'];
for (var i in array) {
console.log(typeof i, i, array[i]);
}
</code></pre>
结果是:
string 0 tt
string 1 dl
string 2 king
然后 for in 形式遍历对象
<pre><code>
object = {code: 200, msg: 'OK'}
for (var obj in object) {
console.log(typeof obj, obj, object[obj]);
}
</code></pre>
结果是:
string code 200
string msg OK
发现了没? for in 遍历数组返回的是数组的下标,而遍历
网友评论