合并基于子数组索引的数组数组作为键(NodeJS/Javascript)

如何编写代码以以下方式合并我的列表?性能很重要。我想转换以下数组:


"list": [

    [

        "marketing",

        "page_sections",

        "PageOne"

    ],

    [

        "marketing",

        "page_sections",

        "PageTwo"

    ],

    [

        "webapp",

        "page",

        "pageone"

    ],

    [

        "webapp",

        "page",

        "pagetwo"

    ],

改成下面的格式:


[   

    {

     name: "marketing",

     path: "marketing/",           

     children: [

                    {

                        name: "page_sections",

                        path: "marketing/page_sections", 

                        children: [

                            {

                                name: "pageOne",

                                path: "marketing/page_sections/pageOne", 

                                children: []

                            },

                            {

                                name: "pageTwo",

                                path: "marketing/page_sections/pageTwo", 

                                children: []

                            },

                       }

           ],

     },

    {

     name: "webapp",

     path: "webapp/"

     children: [

                  {

                    name: "page",

                    path: "webapp/page/"

                    

                    children: [

                        {

                            name: "pageone",

                            path: "webapp/page/pageone"

                            children: []

                        },

                        {

                            name: "pagetwo",

                            path: "webapp/page/pagetwo"

                            children: []

                        },

                    }

             ]

     },

]

子数组的第一个索引是父级,第二个索引是父级的子级,第三个索引是第二个索引的子级(依此类推)。


慕妹3242003
浏览 143回答 4
4回答

MM们

最短的方法是迭代嵌套名称并查找具有相同名称的对象。如果不存在,则创建一个新对象。将数组返回children为新级别。此方法的特点Array#reduce是迭代外部数组data和所有内部数组。const    data = [["marketing", "page_sections", "PageOne"], ["marketing", "page_sections", "PageTwo"], ["webapp", "page", "pageone"], ["webapp", "page", "pagetwo"]],    result = data.reduce((r, names) => {        names.reduce((level, name, i, values) => {            let temp = level.find(q => q.name === name),                path = values.slice(0, i + 1).join('/') + (i ? '' : '/');            if (!temp) level.push(temp = { name, path, children: [] });            return temp.children;        }, r);        return r;    }, []);console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }

哔哔one

查看来源以及您的预期结果。我要做的是循环list,然后在列表中进行另一个循环。将此与Array.find..混合例如..const data = {list:[  ["marketing","page_sections","PageOne"],  ["marketing","page_sections","PageTwo"],  ["webapp","page","pageone"],  ["webapp","page","pagetwo"]]};  function makeTree(src) {  const root = [];  for (const s of src) {    let r = root;    let path = '';    for (const name of s) {      path += `${name}/`;      let f = r.find(k => k.name === name);      if (!f) r.push(f = {name, path, children: []});      r = f.children;    }  }  return root;}   console.log(makeTree(data.list));.as-console-wrapper {  min-height: 100%;}

鸿蒙传说

您可以执行以下操作,list= [    [        "marketing",        "page_sections",        "PageOne"    ],    [        "marketing",        "page_sections",        "PageTwo"    ],    [        "webapp",        "page",        "pageone"    ],    [        "webapp",        "page",        "pagetwo"    ],  ];getChildrenItem = (arr) => {  if(arr.length === 1) {    return { name: arr[0], children: []};  } else {    return { name: arr.splice(0,1)[0], children: [getChildrenItem([...arr])]};  }  }merge = (srcArr, newObj) => {  const {name, children} = newObj;  let index = srcArr.findIndex(item => item.name === name);  if( index> -1) {    children.forEach(item => merge(srcArr[index].children, item))    return ;  } else {    srcArr.push(newObj);    return;  }}allObj = [];list.forEach(item => {  let tempObj = getChildrenItem([...item]);  merge(allObj, tempObj);});console.log(allObj);

翻过高山走不出你

如果性能是一个问题,我认为这是最好的解决方案之一。let list = [&nbsp; ["marketing", "page_sections", "PageOne"],&nbsp; ["marketing", "page_sections", "PageTwo"],&nbsp; ["webapp", "page", "pageone"],&nbsp; ["webapp", "page", "pagetwo"],];const dt = {};const pushToOBJ = (Object, name) => {&nbsp; if (Object[name]) return Object[name];&nbsp; Object[name] = {&nbsp; &nbsp; name,&nbsp; &nbsp; children: {},&nbsp; };&nbsp; return Object[name];};for (let i = 0; i < list.length; i++) {&nbsp; let subArray = list[i];&nbsp; let st = pushToOBJ(dt, subArray[0]);&nbsp; for (let j = 1; j < subArray.length; j++) {&nbsp; &nbsp; st = pushToOBJ(st.children, subArray[j]);&nbsp; }}let result = [];const convertObjToChildArray = (obj) => {&nbsp; if (obj === {}) return [];&nbsp; let arr = Object.values(obj);&nbsp; for (let i = 0; i < arr.length; i++) {&nbsp; &nbsp; arr[i].children = convertObjToChildArray(arr[i].children);&nbsp; }&nbsp; return arr;};result = convertObjToChildArray(dt);console.log(result);没有使用 JS find 函数,该函数已经具有 O(n) 复杂度。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript