js中for循环的用法

用for循环取数组的值

第一种

    <script>
        // 用for循环取数组的值
        function f1(){
            let a = ['', '', '', '', ''];
            for (let item in a){
                // console.log(a[item]);
                let s = a[item] + "<br/>";
                document.write(s);
        }
        }
        setInterval('f1();', 500);
    <script/>

第二种

<script>
        function f1(){
            let a = ['', '', '', '', ''];
            for (let item=0; item<a.length; item++){
                // console.log(a[item]);
                let s = a[item] + "<br/>";
                document.write(s);
        }
        }
        setInterval('f1();', 500);
</script>

 

 

用for循环取对象的值

    <script>
        //用for循环取对象的值
        function f1(){
            let a = {'k1':'', 'k2':'', 'k3':'', 'k4':'','k5':''};
            for (let item in a){
                // console.log(a[item]);
                let s = a[item] + "<br/>";
                document.write(s);
        }
        }
        setInterval('f1();', 500)
    </script>

 

posted @ 2019-02-23 11:12  平平无奇小辣鸡  阅读(4527)  评论(0)    收藏  举报