美文网首页
2018-11-12 How to loop through N

2018-11-12 How to loop through N

作者: 五大RobertWu伍洋 | 来源:发表于2018-11-12 11:00 被阅读9次
    1. for v in obj/array , works on both obj(like a map) and array:

    for obj:

    var user = {
        first_name: "CSS",
        last_name: "HTML",
        age: 4,
        /*from www.w3cschool.cn*/
        website: "www.w3cschool.cn"
    };
    
    > for(v in user) console.log(v);
    first_name
    last_name
    age
    website
    
    > for(v in user) console.log(user[v]);
    CSS
    HTML
    4
    www.w3cschool.cn
    
    //obj does not allow `for of` statement
    > for(v of user) console.log(v);
    TypeError: user is not iterable
    

    for array:

    > for(v of arr) console.log(v);
    10
    9
    8
    
    > arr=[10,9,8]
    [ 10, 9, 8 ]
    > for(v in arr) console.log(arr[v]);
    10
    9
    8
    
    //`for of` works in array since array can be iterated
    > for(v of arr) console.log(v);
    10
    9
    8
    

    相关文章

      网友评论

          本文标题:2018-11-12 How to loop through N

          本文链接:https://www.haomeiwen.com/subject/oppqfqtx.html