首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从索引创建父子嵌套JSON

从索引创建父子嵌套JSON
EN

Stack Overflow用户
提问于 2017-07-05 14:16:24
回答 1查看 1.1K关注 0票数 0

我是D3的新手。我使用的是d3.jd版本3。我有以下类型的JSON:

代码语言:javascript
复制
[
  {
    "health": "OK",
    "name": "new A",
    "index": "A"
  },
  {
    "health": "SEVERE",
    "name": "new AB",
    "index": "A > B"
  },
  {
    "health": "OK",
    "name": "new ABC",
    "index": "A > B > C"
  },
  {
    "health": "OK",
    "name": "new ADE",
    "index": "A > D > E"
  },
  {
    "health": "SEVERE",
    "name": "new AD",
    "index": "A > D"
  }
]

我想从上面的JSON创建以下父子嵌套数据:

代码语言:javascript
复制
[
  {
    "health": "OK",
    "name": "new A",
    "index": "A",
    "children": [
      {
        "health": "SEVERE",
        "name": "new AB",
        "index": "A > B",
        "children": [
          {
            "health": "OK",
            "name": "new ABC",
            "index": "A > B > C"
          }
        ]
      },
      {
        "health": "SEVERE",
        "name": "new AD",
        "index": "A > D",
        "children": [
          {
            "health": "OK",
            "name": "new ADE",
            "index": "A > D > E"
          }
        ]
      }
    ]
  }
]

我想通过索引创建父子数据。因此,如果索引是"A > B“,"A”是父级,"B“是子级。

这只是我没有的样本数据。大量的数据。所以,想要提高性能。

是否可以使用d3或jquery来创建嵌套的父子JSON形式的object数组?

EN

回答 1

Stack Overflow用户

发布于 2017-07-06 13:40:25

我已经做到了。为此,我需要添加父id。

代码语言:javascript
复制
var data = [
  {
    "health": "OK",
    "name": "new A",
    "index": "A"
  },
  {
    "health": "SEVERE",
    "name": "new AB",
    "index": "A > B"
  },
  {
    "health": "OK",
    "name": "new ABC",
    "index": "A > B > C"
  },
  {
    "health": "OK",
    "name": "new ADE",
    "index": "A > D > E"
  },
  {
    "health": "SEVERE",
    "name": "new AD",
    "index": "A > D"
  }
]

  function fnGetParentID(index) {
                     var indexArr = index.split(' > ');
                     var parentIndex = '';
                     if(indexArr.length !== 1){
                         parentIndex = index.replace(" > " + indexArr[indexArr.length - 1], '')
                     }
                     return parentIndex;
                 };

 function fnCreateStrunture(f) {
                    return f.sort(function (a, b) {
                        return a.index.length < b.index.length ? 1 : a.index.length == b.index.length ? a.index < b.index ? -1 : 1 : -1;
                    }).reduce(function (p, c, i, a) {
                        c.ParentID = fnGetParentID(c.index);
                        var parent = !!c.ParentID && a.find(function (e) {
                                return e.index === c.ParentID;
                            });
                        ? parent.children && parent.children.push(c) || (parent.children = [c]) : p.push(c);
                        return p;
                    }, []);
                };
                           
var nestedJson = fnCreateStrunture(data);

document.getElementById("nested").innerHTML = JSON.stringify(nestedJson, undefined, 2);
代码语言:javascript
复制
<pre id="nested"></pre>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44918298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档