JS数组遍历

### 三  数组的遍历
( 1 )  for 循环

let ary = ['a', 'b', 'c', 'd', 12, 3, 4, 'f'];
for (let i = 0; i < ary.length; i++) {
    console.log(ary[i]);
}
( 2 ) for     of  (ES6 )

let ary = ['a', 'b', 'c', 'd', 12, 3, 4, 'f'];
for(let item of ary){
    console.log(item);
}

( 3 ) for      Each

ary.forEach(function(item, index){
    console.log(index);
});


判断是不是数组

let ary = [[1, 2, 4], 5, [8, 9, 10]];

for (let item of ary) {
    if (Array.isArray(item)) {   (只要是这句判断)
        for (let value of item) {
            console.log(value);
        }
    } else {
        console.log(item);
    }
}

posted @ 2018-07-22 23:15  Fen达  阅读(105)  评论(0编辑  收藏  举报