2012-12-04 111 views
0

我需要窩我在JSON創建另一個數組的一個數組裏面,給它適當的格式化爲jQuery Datatables library.如何在JavaScript中嵌套另一個數組中的數組?

這裏是我當前的代碼:

$.ajax({ 
    url: uMtbls, 
    dataType: 'json', 
    success: function(jMtbl) { 
     $.each(jMtbl, function(i, item) { 
      mAll = jMtbl[i]; 
      mOwn = jMtbl[i].OrigOwner; 
      mPub = jMtbl[i].PublicationTblNm; 
      mTbl = jMtbl[i].TableId; 
      mMig = jMtbl[i].MigrationFreq; 
      mGTYPE = jMtbl[i].GTYPE; 
      exec(); 

     }); 

     oTable = $('#dtManaged').dataTable({ 
      "bStateSave": true, 
      "bProcessing": true, 
      "bServerSide": true, 
      "aaData": jReform, 
      "aoColumns": [{ 
       "sName": mOwn, 
       "sTitle": "Original Owner", 
       "sWidth": "10%" 
      }, { 
       "sName": mPub, 
       "sTitle": "Table Name" 
      }, { 
       "sName": mTbl, 
       "sTitle": "ID" 
      }, { 
       "sName": mMig, 
       "sTitle": "Migration Frequency" 
      }, { 
       "sName": mGTYPE, 
       "sTitle": "Oracle Gtype" 
      }], 
      "sPaginationType": "full_numbers", 
      "iDisplayLength": 25, 
      "aLengthMenu": [ 
       [25, 50, 100, -1], 
       [25, 50, 100, "All"] 
      ] 
     }); 
    } 
}); 

function exec() { 
    jBld = mOwn + "," + mPub + "," + mTbl + "," + mMig + "," + mGTYPE; 
    //console.debug("[" + mOwn + "," + mTbl + "]"); 
    jReform = jBld.split(","); 
    console.debug(jReform); 
}​ 

輸出看起來像這樣(在捕獲console.debug):

["text","text","text","text","text"] 
["text","text","text","text","text"] 

樣品應該是:

{ "aaData": [ 
    [ 10126, 10002253, 415 ] 
]} 
+2

你爲什麼不解碼JSON到一個Javascript變量,然後操縱數據你想要嗎? – thatidiotguy

+0

這就是我目前正在做的。我遇到了嵌套數組的問題,我在另一個數組中創建數組,以便將數據轉換爲所需的格式 – atlMapper

+1

然後請僅顯示該代碼。 AJAX方面則無關緊要。只顯示必要的代碼。 – thatidiotguy

回答

1

使用Array.push。並利用var聲明,您正在創建一個全局變量的地獄。

var aaData = []; 
$.each(jMtbl, function (i, item) { 
    aaData.push([ 
    item.OrigOwner, 
    item.PublicationTblNm, 
    item.TableId, 
    item.MigrationFreq, 
    item.GTYPE 
    ]); 
}); 

oTable = $('#dtManaged').dataTable({ 
    ... 
    "aaData": aaData, 
    ... 
}); 

(注:這些鏈接不僅節目,請通過他們讀)

+0

謝謝!我沒有機會讓我的代碼被檢查(永遠),所以我狂野的西部,這不是一件好事...謝謝你解決了我的問題,今晚我會做一些閱讀 – atlMapper

相關問題