为了账号安全,请及时绑定邮箱和手机立即绑定

如何在js中制作父子json树

如何在js中制作父子json树

青春有我 2021-04-11 16:13:11
我有这样的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 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞


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));

Second Case, without { target: 'a', source: '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));

Second Case, without { target: 'a', source: '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));

Second Case, without { target: 'a', source: '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));


查看完整回答
反对 回复 2021-04-29
?
浮云间

TA贡献1829条经验 获得超4个赞

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);

动态添加到JSON json_2['target'] = 'c';


查看完整回答
反对 回复 2021-04-29
  • 2 回答
  • 0 关注
  • 194 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信