创建JSON对象的层次结构数组

时间:2014-09-22 14:25:14

标签: javascript arrays json

我几乎完成了JavaScript代码,以创建我的最终JSON格式对象数组。 从我的源数组中,我创建了一个顶级父级及其各自的子记录。

我现在需要将其他记录推送到一个或多个子记录上。这是我遇到麻烦的地方。

这是我的目标数据,其中“id”:29是我的顶级节点。然后“儿童”阵列落在下面:

 [
  {
    "id": 29,
    "field0": "$ALL$",
    "field1": 1564.92711931719,
    "field2": -171.934775655824,
    "field3": -292.877167373888,
    "children": [
        {
            "id": 0,
            "field0": "Goldman",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 1,
            "field0": "IBM",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 2,
            "field0": "JP Chase",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 4,
            "field0": "Merrill",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 5,
            "field0": "Nokia",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 6,
            "field0": "Pequot",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 9,
            "field0": "Deutsche",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 16,
            "field0": "General Insurance",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 18,
            "field0": "AIG",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 19,
            "field0": "Andor",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 20,
            "field0": "BARCLAYS",
            "field1": 92.7731197209214,
            "field2": -10.1717767200607,
            "field3": 616.369427825382
        },
        {
            "id": 26,
            "field0": "CSFB",
            "field1": 3.35029024626419,
            "field2": -0.367366071961442,
            "field3": -132.725310207512
        },
        {
            "id": 27,
            "field0": "BLOSOFL",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 28,
            "field0": "GRMOBND",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 32,
            "field0": "GM",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 33,
            "field0": "GE",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        {
            "id": 34,
            "field0": "BHPBMGRP",
            "field1": 1468.80370935,
            "field2": -161.395632863801,
            "field3": -776.521284991759                
        },
        {
            "id": 35,
            "field0": "PIMCO",
            "field1": 0,
            "field2": 0,
            "field3": 0
        }
    ]
  }
 ]

我现在留下了以下剩余的源数组,我需要在其中找到上面的父记录。

[   
{
    "id": 7,
    "parent": 35,
    "values": [
        "Pimco Fund A",
        0,
        0,
        0
    ]
},

{
    "id": 15,
    "parent": 0,
    "values": [
        "GSAM",
        0,
        0,
        0
    ]
},
{
    "id": 17,
    "parent": 33,
    "values": [
        "Genworth",
        0,
        0,
        0
    ]
},
{
    "id": 21,
    "parent": 30,
    "values": [
        "BHHSH",
        0,
        0,
        0
    ]
},
{
    "id": 22,
    "parent": 30,
    "values": [
        "BHPBFIN",
        0,
        0,
        0
    ]
},
{
    "id": 23,
    "parent": 31,
    "values": [
        "BHPSTEEUR",
        1468.80370935,
        -161.395632863801,
        -776.521284991759
    ]
},
{
    "id": 25,
    "parent": 31,
    "values": [
        "BLUESCOPEFIN",
        0,
        0,
        0
    ]
},
{
    "id": 30,
    "parent": 34,
    "values": [
        "BHPBGRP",
        0,
        0,
        0
    ]
},
{
    "id": 31,
    "parent": 34,
    "values": [
        "BHPSTEELGRP",
        1468.80370935,
        -161.395632863801,
        -776.521284991759
    ]
}

我可以使用UnderscoreJS库来查找“剩余”中的每个项目,但是如何将它们推送到我的parent []数组中?

更新:

这是我的_.each()循环,它最终创建了子项;但是,我的剩余阵列中仍然存在一些错误,最终会成为孩子的孩子(n级层次结构)

findPar["children"] = thisChild[0]; 

我相信有一种更聪明的方法可以实现这一目标。

       var findPar;
        _.each(rowsNew, function (row) {
              // looks through parents array where id == row.parent
            findPar = _.findWhere(parents[0].children, { id: row.parent }); 
            if (findPar != undefined) {                          
                if (findPar.children == undefined) {
                    findPar["children"] = createJsonFromSingleRow(row);
                }
                else {
                    findPar.children.push(createJsonFields(row));
                }
                rowsNew = _.reject(rowsNew, function (rec) { return rec.id == row.id;  });                }
        });
        return newJson = parents;

提前感谢您的帮助。

鲍勃

1 个答案:

答案 0 :(得分:2)

如果使用id作为属性名称的对象而不是数组,则会更简单一些。 (您可以创建一个函数来从原始JSON对象执行此转换)

var hash = {
  '29' : {
    "id": 29,
    "field0": "$ALL$",
    "field1": 1564.92711931719,
    "field2": -171.934775655824,
    "field3": -292.877167373888,
    "children": {
        '0': {
            "id": 0,
            "field0": "Goldman",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        '1': {
            "id": 1,
            "field0": "IBM",
            "field1": 0,
            "field2": 0,
            "field3": 0
        },
        ...
        '35': {
            "id": 35,
            "field0": "PIMCO",
            "field1": 0,
            "field2": 0,
            "field3": 0
        }
    }
  }
 };

然后你可以像这样将每个剩余的东西添加到父哈希中(使用普通的JS并假设你已经找到并填充了具有类似哈希结构的剩余部分):

for( key in leftovers ){
  if( leftovers.hasOwnProperty(key) ){
    var parent = leftovers[key].parent;

    // If parent doesn't exist, create it
    if( !parent in hash ) {
      hash[parent] = { 
        id: parent,
        children: {}
      };
    }

    // Create the children object if it doesn't exist
    if( !hash[parent].children ) {
      hash[parent].children = {};
    }

    // Copy the object from leftovers into the parent hash
    // copyObj is custom and I didn't include it here.
    hash[parent].children[key] = copyObj(leftovers[key]);  

    // Remove from leftovers, now that it's moved to the parent hash.
    delete leftovers[key];
  }
}