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

标签 javascript arrays json

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

我现在需要将附加记录推送到多个子记录之一。这就是我遇到麻烦的地方。

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

 [
  {
    "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 库来定位“剩余”中的每个项目,但如何将它们推送到我的parents[]数组上?

更新:

这是我的 _.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;

预先感谢您的帮助。

鲍勃

最佳答案

如果您使用以 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];
  }
}

关于javascript - 创建 JSON 对象的层次结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25976477/

相关文章:

javascript - 如何在 MongoDB 中检索子文档引用

Javascript:函数、对象、new ....有点困惑

ios - 如何通过 iOS 通知中心发送和接收 JSON?

php - 根据另一个表中的值从一个表中选择值

c++ - 使用 Mergesort 计算 C++ 中的反转次数

javascript - Handsontable JSON 返回未定义

javascript - 维特斯 : WebSocket connection to 'wss://host:port/' failed due to HMR

javascript - 动态添加选项到select2

PHP 计算某个操作每周发生了多少次

将对象转换为数组的 PHP 错误