猿问

如何在js中制作父子json树

我有这样的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写的


青春有我
浏览 195回答 2
2回答

慕运维8079593

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

浮云间

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';
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答