-3

私が与えた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"
        }} ];
4

1 に答える 1

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;
}

input は開始オブジェクトであり、必要な構造で出力を返します。または、少なくともあなたが望んでいたと思うもの。

于 2012-10-30T06:39:22.770 に答える