Jquery 的Each作法

以前都是用for迴圈再做東西,但是後來發現有一些同事都是使用each來作業
便在今天試著使用了一下each來做api串接

一維的for基本型態
for (var i = 0; i < FateDataRange_Array.length; i++) {}
一維的each作法
$.each(FateDataRange_Array,function(i,object){
    console.log(i+' = '+object);
});

二維的for基本型態
for (var i = 0; i < data.GetCountryListResult.KVLlists.length; i++) {
    data.GetCountryListResult.KVLlists[i].key
    data.GetCountryListResult.KVLlists[i].val}
二維的each作法
$.each(data.GetCountryListResult.KVLlists,function (i,object) {
    console.log(i+' = '+object.key+' = '+object.val);
    console.log(data.GetCountryListResult.KVLlists.length);//長度
});


參考資料:
http://g2room.com/index.php/archives/62
http://www.g2room.com/jquery/example/base/each.html

留言

這個網誌中的熱門文章

jQuery獲取Select選擇的Text和Value(轉)

Android 在Fragment下控制輸入鍵盤

彈跳視窗iframe的運用