如何在js中制作父子json树

时间:2019-04-12 22:52:44

标签: javascript

我有这样的json:

data=[ { target: 'a', source: 'a' }
     , { target: 'a', source: 'b' }
     , { target: 'b', source: 'c' }
     , { target: 'c', source: 'd' }
     ];

但我想要

data= { "target": "a", "children": [
        { "target": "b", "children": [
          { "target": "c", "children": [
           { "target": "d" } ] } ] };

你怎么用js编写

2 个答案:

答案 0 :(得分:0)

不确定这是最好的代码,但是它可以工作

const
  data = [ { target: 'a', source: 'a' }
          , { target: 'a', source: 'b' }
          , { target: 'b', source: 'c' }
          , { target: 'c', source: 'd' }
          ];

let
  CurKey       = '',
  CurrentChild = null,
  result       = null;
    
data.forEach( elm => {

  if (elm.target === elm.source) 
  {
    CurKey = elm.target; 
    result = { "target": CurKey, "children": [] };
    CurrentChild = result.children;
  }
  else if (elm.target === CurKey)
  {
    CurKey = elm.source;

    if (data.findIndex(ef=>ef.target===elm.source) >0 )
    {
      let newElm = { "target": CurKey, "children": [] };
      CurrentChild.push(newElm)
      CurrentChild = newElm.children;
    }
    else
    {
      let newElm = { "target": CurKey };
      CurrentChild.push(newElm)
      CurrentChild = null;
      CurKey = null;
    }
  }

});

console.log(JSON.stringify(result));

第二种情况,不包含{目标:“ a”,来源:“ a”}:

const
  data =  [ { target: 'a', source: 'b' }
          , { target: 'b', source: 'c' }
          , { target: 'c', source: 'd' }
          ];

let
  CurKey       = '',
  CurrentChild = null,
  result       = null;
    
data.forEach( elm => {

  if (CurKey === '') 
  {
    CurKey = elm.target; 
    result = { "target": CurKey, "children": [] };
    CurrentChild = result.children;
  }
  if (elm.target === CurKey)
  {
    CurKey = elm.source;

    if (data.findIndex(ef=>ef.target===elm.source) >0 )
    {
      let newElm = { "target": CurKey, "children": [] };
      CurrentChild.push(newElm)
      CurrentChild = newElm.children;
    }
    else
    {
      let newElm = { "target": CurKey };
      CurrentChild.push(newElm)
      CurrentChild = null;
      CurKey = null;
    }
  }

});

console.log(JSON.stringify(result));

答案 1 :(得分:0)

    @{
        ViewBag.Title = "Index";
     }

    @{
        var leap_year = "";
        var error_mes = "";    
        if (IsPost)
        {
           var yr = Request["year"];
           if (yr.AsInt() < 0) 
           {
              error_mes = "Please Input positive integer";
           } 
           else if ((yr.AsInt()%4 == 0) || (yr.AsInt()%400 == 0))
           {
              leap_year = yr + " is leap year";
           }
           else
           {
              leap_year = yr + " is not leap year";
           }
        }
     }

     <html>
     <head>
        <title>Leap Year</title>

     </head>
     <body>
        <h3>Please input to check for leap year</h3>
        <form method="post">
           <label for="year">Year</label><br />
           <p><input type="text" name="year" /></p>
           <p><input type="submit" value="Check" /></p>
        </form>
        <p>@error_mes @leap_year</p>
     </body>
     </html>

动态添加到JSON var arr_1 = []; var arr_2 = []; var arr_3 = []; var json = {}; var json_2 = {}; var json_3 = {}; var json_4 = {}; arr_3.push(json_4); json_3.target = 'c'; json_3.children = arr_3; arr_2.push(json_3); json_2.target = 'b'; json_2.children = arr_2; arr_1.push(json_2); json.target = 'a'; json.children = arr_1; console.log(json);