js集合的三种遍历方式

"user strict";

window.onload = function() {
    var ary = ['a', 'b', 'c'];

    // for...in
    console.log("for...in");
    for (var i in ary) {
        console.log(ary[i]);
    }

    // for...of
    console.log("for...of");
    for (var value of ary) {
        console.log(value);
    }

    // forEach
    console.log("forEach");
    ary.forEach(function(ele) {
    console.log(ele);
    });
}

输出结果

for...in
a
b
c
for...of
a
b
c
forEach
a
b
c

你可能感兴趣的:(js)