1

我的 php 脚本中有 JSON 字符串,如下所示:

var r.co = {
                "A20018425":[
                                {"balance":"1390.31"},      // 1
                                {"balance":"1304.11"},      // 2
                                {"balance":"1188.11"},      // 3
                                {"balance":"1421.71"}       // 4
                            ],

                "A25005922":[
                                {"balance":"1000"},         // 1
                                {"balance":"1000.86"},      // 2
                                {"balance":"986.32"},       // 3
                                {"balance":"988.96"},       // 4
                                {"balance":"980.26"},       // 5
                                {"balance":"980.16"}        // 6 MAX 
                            ],

                "A25005923":[
                                {"balance":"1001"},         // 1
                                {"balance":"1000.16"},      // 2
                            ]           
            }

我不知道有多少个 AXXXXXXXX 元素,它包含多少个元素。要获得 A 元素,我使用以下代码:

var accounts = [];
for(var key in r.co) {
   if(r.co.hasOwnProperty(key)) {
      accounts.push(key);
   }
}

现在我知道我的 A 元素长度

var accounts_length = accounts.length; // 3 for example

现在我需要知道 A 中元素的最大长度:

var accounts_elements_length = [];

    for (var c = 0; c < accounts.length; c++) {   

          accounts_elements_length.push(r.co[accounts[c]].length);
    }

    var accounts_elements_length_max = accounts_elements_length.max() // 6 For example

我怎样才能得到这个图表的输出数组?

var outputData = [{
                        count:      1,
                        A20018425:  1390.31,
                        A25005922:  1000,
                        A25005923:  1001
                    }, {
                        count:      2,
                        A20018425:  1304.11,
                        A25005922:  1000.86,
                        A25005923:  1000.16
                    }, {
                        count:      3,
                        A20018425:  1188.11,
                        A25005922:  986.32
                    }, {
                        count:      4,
                        A20018425:  1421.71,
                        A25005922:  988.96
                    }, {
                        count:      5,
                        A25005922:  980.26
                    }, {
                        count:      6,
                        A25005922:  980.16
                    }
        }];

谢谢!

4

2 回答 2

2

刚刚结合了你的算法:

var outputData = [];
for (var key in r.co) {
   if (r.co.hasOwnProperty(key)) {
      var account_length = r.co[key].length;
      for (var c = 0; c < account_length; c++) {
          if (outputData[c] === undefined) {
              outputData[c] = { count: c+1 };
          }
          outputData[c][key] = r.co[key][c].balance;
    }
   }
}
console.log(outputData);

jsfiddle

于 2012-10-19T12:03:49.183 回答
1

代码 :

var outputData = [];
for (var i = 0; i < 6; i++) { // filter should be - i < accounts_elements_length_max
    var temp = {
        'count': i + 1
    };

    for (var j = 0; j < accounts.length; j++) {
        if (r[accounts[j]][i]) temp[accounts[j]] = r[accounts[j]][i].balance;
    }

    outputData.push(temp);
}

请注意,我对 accounts_elements_length_max(6) 进行了硬编码。

我得到的输出:

[{
    "count": 1,
    "A20018425": "1390.31",
    "A25005922": "1000",
    "A25005923": "1001"},
{
    "count": 2,
    "A20018425": "1304.11",
    "A25005922": "1000.86",
    "A25005923": "1000.16"},
{
    "count": 3,
    "A20018425": "1188.11",
    "A25005922": "986.32"},
{
    "count": 4,
    "A20018425": "1421.71",
    "A25005922": "988.96"},
{
    "count": 5,
    "A25005922": "980.26"},
{
    "count": 6,
    "A25005922": "980.16"}]​

工作小提琴

于 2012-10-19T11:51:37.393 回答