【问题标题】:Define a Hierarchy Json Structure定义层次结构 Json 结构
【发布时间】:2018-06-14 14:54:00
【问题描述】:

我有一个 Web 服务,它从 db 上的选择中返回一个 json。 问题是这种对象是层次结构的一部分,其中每个子对象都通过一个名为“idCdcParent”的字段与父对象连接。 根元素是 idCdcParent 为空的位置。 在这种情况下,罗马、米兰和那不勒斯是根元素。 这是json:

[{

  "idCdc": "2",
  "cdcName": "Roma",
  "order": "1",
  "isUsed": "false"
}, {

  "idCdc": "17",
  "idCdcParent": "5",
  "cdcName": "testGP",
  "order": "1",
  "isUsed": "false"
}, {

  "idCdc": "5",
  "idCdcParent": "2",
  "cdcName": "Progetti",
  "order": "2",
  "isUsed": "false"
}, {

  "idCdc": "18",
  "idCdcParent": "5",
  "cdcName": "testGPS",
  "order": "2",
  "isUsed": "false"
}, {

  "idCdc": "3",
  "cdcName": "Milano",
  "order": "4",
  "isUsed": "false"
}, {

  "idCdc": "7",
  "idCdcParent": "3",
  "cdcName": "l",
  "order": "4",
  "isUsed": "false"
}, {

  "idCdc": "4",
  "cdcName": "Napoli",
  "order": "5",
  "isUsed": "false"
}, {

  "idCdc": "9",
  "idCdcParent": "4",
  "cdcName": "cccc",
  "order": "6",
  "isUsed": "false"
}]

我希望它是一个具有层次结构的 json,其中字段“idCdcParent”指的是父对象的 id。 像这样:

[{

    "idCdc": "2",
    "cdcName": "Roma",
    "order": "1",
    "isUsed": "false",
    "children": [{

        "idCdc": "5",
        "idCdcParent": "2",
        "cdcName": "Progetti",
        "order": "2",
        "isUsed": "false"
      },
      {

        "idCdc": "17",
        "idCdcParent": "5",
        "cdcName": "testGP",
        "order": "1",
        "isUsed": "false"
      }
    ]
  },
  {

    "idCdc": "5",
    "idCdcParent": "2",
    "cdcName": "Progetti",
    "order": "2",
    "isUsed": "false"
  },
  {

    "idCdc": "3",
    "cdcName": "Milano",
    "order": "4",
    "isUsed": "false",
    "children": [{
      "idCdc": "7",
      "idCdcParent": "3",
      "cdcName": "l",
      "order": "4",
      "isUsed": "false"
    }]
  },
  {
    "idCdc": "4",
    "cdcName": "Napoli",
    "order": "5",
    "isUsed": "false",
    "children": [{


      "idCdc": "9",
      "idCdcParent": "4",
      "cdcName": "cccc",
      "order": "6",
      "isUsed": "false"
    }]
  }
]

在javascript中这可能吗? 谢谢

【问题讨论】:

    标签: javascript json hierarchy


    【解决方案1】:

    确定有可能,只需遍历数组并为 id 建立一个查找表:

    const byID = {};
    
    for(const el of array) 
      byId[el.idCdc] = el;
    

    现在您有了查找表,很容易遍历节点,并将它们转换为层次结构:

    let root;
    
    for(const el of array) {
      if(el.idCdcParent) {
         const parent = byID[el.idCdcParent];
         if(!parent.children) parent.children = [];
         parent.children.push(el);
      } else {
         root = el;
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用递归函数将实体数组转换为树。此方法会创建一个新数组的分层实体,并且原始实体不会被修改 - 根据您的特定用例,可能是正数或负数:

      function make_tree(treeable_entities) {
        function TreeNode(entity) {
          const children = get_children(entity.idCdc);
          const new_entity = Object.assign({}, entity);
          new_entity.children = children; 
          return new_entity;
        }
      
        function get_children(ofIdentifier) {
          return treeable_entities.filter(entity => entity.idCdcParent === ofIdentifier).map(TreeNode);
        }
      
        const is_root = (entity) => entity. idCdcParent === undefined;
        return treeable_entities.filter(is_root).map(TreeNode);
      };
      

      【讨论】:

      • 我忘了说:如果数据库中的数据有多个父->子关系,这个递归函数将处理无限数量的父->子级。
      • 这对您有帮助吗?你是否让你的父母 -> 孩子的关系按照你想要的方式运作?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2015-01-09
      • 2021-09-25
      相关资源
      最近更新 更多