将字符串树转换为数组

我有一个 id 作为字符串的对象。每个对象都可以是另一个对象的子对象。可以从 ID 猜测关系。举个例子:


[

  { id: '1:2:6', ids: ['1', '2', '6'] },

  { id: '1:4', ids: ['1', '4'] },

  { id: '1', ids: ['1'] },

  { id: '1:2', ids: ['1', '2'] },

]

在这个例子中,根对象是id: 1,它有 2 个孩子id: 1:2和id: 1:4。终于,id: 1:2有了孩子id: 1:2:6。


我想将此数组转换为另一个数组,其中儿童嵌入到父母中,因此前一个数组将导致:


[

  {

    id: '1',

    children: [

      {

        id: '1:2',

        children: [

          { id: '1:2:6', children: [] }

        ],

      },

      {

        id: '1:4',

        children: [],

      }

    ],

  }

]

我可以使用 ES6。我尝试了几个小时来使用各种循环找到解决方案,但我无法弄清楚。任何帮助,将不胜感激!


素胚勾勒不出你
浏览 151回答 3
3回答

江户川乱折腾

您可以ids通过在实际级别查找对象来迭代对象并减少对象。如果没有找到创建一个新对象。然后让孩子们回来。var data = [{ id: '1:2:6', ids: ['1', '2', '6'] }, { id: '1:4', ids: ['1', '4'] }, { id: '1', ids: ['1'] }, { id: '1:2', ids: ['1', '2'] }],    tree = data.reduce((r, { ids }) => {        ids.reduce((t, _, i, a) => {            var id = a.slice(0, i + 1).join(':'),                temp = t.find(o => o.id === id);                        if (!temp) t.push(temp = { id, children: [] });            return temp.children;        }, r);        return r;    }, []);console.log(tree);.as-console-wrapper { max-height: 100% !important; top: 0; }

侃侃尔雅

使用递归算法构建树var jsonTree = [{ id: '1:2:6', ids: ['1', '2', '6'] },{ id: '1:4', ids: ['1', '4'] },{ id: '1', ids: ['1'] },{ id: '1:2', ids: ['1', '2'] },]var newJsonTree = [];var currentElement = {id: '1',childs: []}newJsonTree.push(currentElement)function buildTree(jsonTree, currentElement){&nbsp; &nbsp; for(var i=0;i<jsonTree.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; var parent = jsonTree[i];&nbsp; &nbsp; &nbsp; &nbsp; for(var j=0;j<jsonTree.length;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var child = jsonTree[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(child['visited'] != true && child['id'] != currentElement['id'] && child['id'].indexOf(currentElement['id']) == 0 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(child['id'].split(":").length == currentElement['id'].split(":").length+1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newElement = {}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newElement['id'] = child['id'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newElement['childs'] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentElement['childs'].push(newElement);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child['visited'] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buildTree(jsonTree, newElement);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}buildTree(jsonTree, currentElement);document.write(JSON.stringify(newJsonTree));结果:[{"id":"1","childs":[{"id":"1:4","childs":[]},{"id":"1:2","childs":[ {"id":"1:2:6","childs":[]}]}]}]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript