DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS代碼實例:遍歷數組成員的迭代器
JS代碼實例:遍歷數組成員的迭代器
編輯:關於JavaScript     

實現一個遍歷數組或對象裡所有成員的迭代器。

var each = function(obj, fn){
 
        //+++++++++++答題區域+++++++++++
 
       
 

 

        //+++++++++++答題結束+++++++++++
 
};
 

try{
 
       
 
        var data1 = [4,5,6,7,8,9,10,11,12];
 
        var data2 = {
 
                "a": 4,
 
                "b": 5,
 
                "c": 6
 
        };
 
       
 
        console.group(data1);
 
       
 
        each(data1, function(o){
 
                if( 6 == this )
 
                        return true;
 
                else if( 8 == this )
 
                        return false;
 
                console.log(o + ": \"" + this + "\"");
 
        });
 
       
 
        console.groupEnd();
 

        /*------[執行結果]------
 

        1: "4"
 
        2: "5"
 
        4: "7"
 

        ------------------*/
 
       
 
        console.group(data2);
 
       
 
        each(data2, function(v, n){
 
                if( 5 == this )
 
                        return true;
 
                console.log(n + ": \"" + v + "\"");
 
        });
 
       
 
        console.groupEnd();
 

        /*------[執行結果]------
 

        a: "4"
 
        c: "6"
 

        ------------------*/
 
       
 
}catch(e){
 
        console.error("執行出錯,錯誤信息: " + e);
 
}

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved