2012-10-30 167 views
-3

我給出JSON對象如下:JSON對象處理

resultTable = [ 
    { 
     id: "005470021180", 
     balance: "0.00", 
     accountName: "Money Master", 
     category: "Banking", 
     currency: "CAD", 
     entitlements: [ 

     ] 
    }, 
    { 
     id: "005470021288", 
     balance: "0.00", 
     accountName: "Money Master", 
     category: "Banking", 
     currency: "CAD" 
    }, 
    { 
     id: "005470044628", 
     balance: "-72,116.01", 
     accountName: "Money Master", 
     category: "Banking", 
     currency: "CAD" 
    }, 
    { 
     id: "40592000495201", 
     accountName: "Business Loan", 
     category: "Borrowing", 
     cad_balance: "0.00" 
    }, 
    { 
     id: "40592000495202", 
     accountName: "Business Loan", 
     category: "Borrowing", 
     cad_balance: "0.00" 
    }, 
    { 
     id: "40592000495203", 
     accountName: "Business Loan", 
     category: "Investing", 
     cad_balance: "0.00" 
    }, 
    { 
     id: "40592000495204", 
     accountName: "Business Loan", 
     category: "INVESTING", 
     cad_balance: "0.00" 
    }, 
    { 
     usd_balance: "1,080.27", 
     id: "55300070", 
     accountName: "Scotia iTRADE", 
     category: "INVESTING", 
     cad_balance: "272,166.59" 
    } 
]; 

我需要在JS的輸出爲:

resulttable = [ 
{ 
    "Banking": { 
     id: "005470021180", 
     balance: "0.00", 
     accountName: "Money Master", 
     category: "Banking", 
     currency: "CAD", 
     entitlements: [ 

     ] 
    }, 
    { 
     id: "005470021288", 
     balance: "0.00", 
     accountName: "Money Master", 
     category: "Banking", 
     currency: "CAD" 
    }, 
    { 
     id: "005470044628", 
     balance: "-72,116.01", 
     accountName: "Money Master", 
     category: "Banking", 
     currency: "CAD" 
    } 
}, 
{ 
    "Borrowing": { 
     id: "40592000495201", 
     accountName: "Business Loan", 
     category: "Borrowing", 
     cad_balance: "0.00" 
    }, 
    { 
     id: "40592000495202", 
     accountName: "Business Loan", 
     category: "Borrowing", 
     cad_balance: "0.00" 
    } 
}, 
{ 
    ""INVESTING":{ 
      { 
      id : "40592000495203", 
      accountName : "BusinessLoan", 
      category : "Investing", 
      cad_balance : "0.00" 
      }, 
      { 
      id : "40592000495204", 
      accountName : "BusinessLoan", 
      category : "INVESTING", 
      cad_balance : "0.00" 
     }, 
     { 
      usd_balance : "1,080.27", 
      id : "55300070", 
      accountName : "ScotiaiTRADE", 
      category : "INVESTING", 
      cad_balance : "272,166.59" 
     }} ]; 
+7

你有什麼試過?你能否正確地格式化你的問題中的代碼? – Blender

+0

看起來他想對數組進行排序.. – user1600124

+0

不,他希望對項目進行分類 – epoch

回答

0

嘗試是這樣的:

function (input) 
{ 
    var output = new Object(); 
    for(var i = 0, len=input.length; i<len; i++) 
    { 
     if(typeof output[input[i].category] === 'undefined') 
     { 
      output[input[i].category] = []; 
     } 
     output[input[i].category].push(input[i]); 
    } 
    return output; 
} 

輸入將是您的起始對象,並且它將返回所需結構中的輸出。 或至少是我認爲你想要的。