简体   繁体   English

将对象数组转换为嵌套对象数组js

[英]Convert an array of objects into an array of nested objects js

So I have a task to properly order the files directory.所以我有一个任务来正确排序文件目录。 For simplicity, all the documents and files are saved as a flat array.为简单起见,所有文档和文件都保存为平面数组。 As an identifier to display nested files I have a property uniqueId , that tells me what are the parent directories.作为显示嵌套文件的标识符,我有一个属性uniqueId ,它告诉我什么是父目录。 Flat array is sorted by uniqueId , so that each child goes after its parent.平面数组按uniqueId排序,因此每个孩子都在其父母之后。

Here is an example of the flat array:下面是一个平面数组的例子:

[{
 id: 1,
 uniqueId: '1',
 name: 'folder1',
 dir: true
},
{
 id: 2,
 uniqueId: '1.2',
 name: 'test1',
 dir: false
},
{
 id: 3,
 uniqueId: '1.2.3',
 name: 'test2'
 dir: false
},
{
 id: 4,
 uniqueId: '1.2.4',
 name: 'test3'
 dir: true
},
{
 id: 3,
 uniqueId: '1.3',
 name: 'test23'
 dir: true
},
{
 id: 1,
 uniqueId: '1.3.1',
 name: 'test6',
 dir: true
},
]

Which basically represents files directory tree:它基本上代表文件目录树:

1
  1.2
    1.2.3
    1.2.4
  1.3
    1.3.1

I need to display directories first, ie those that have dir: true .我需要首先显示目录,即具有dir: true的目录。 Thus, the above tree would become:因此,上面的树将变为:

1
 1.3
    1.3.1
 1.2
    1.2.4
    1.2.3

So as a solution I decided that it's better to convert a flat array into nested object, sort children of each object in the desired way and then transform into flat array back again.因此,作为一种解决方案,我决定最好将平面数组转换为嵌套的 object,以所需的方式对每个 object 的子项进行排序,然后再次转换为平面数组。 So that my flat array would become like this:这样我的平面数组就会变成这样:

{
 id: 1,
 uniqueId: '1',
 name: 'folder1',
 dir: true
 childs: [
  {
   id: 2,
   uniqueId: '1.2',
   name: 'test1',
   dir: false,
   childs: [
   {
     id: 3,
     uniqueId: '1.2.3',
     name: 'test2'
     dir: false
   },
   {
     id: 4,
     uniqueId: '1.2.4',
     name: 'test3'
     dir: true
   }
 ]},
 {
   id: 3,
   uniqueId: '1.3',
   name: 'test23'
   dir: true,
   childs: [
    {
     id: 1,
     uniqueId: '1.3.1',
     name: 'test23'
     dir: true
    }
  ]
}
}]

I can't find a way to convert flat into the desired nested object.我找不到将平面转换为所需嵌套 object 的方法。 I already have a function that returns children of the current object isChild(parent, objectToCheck) .我已经有一个 function 返回当前 object isChild(parent, objectToCheck)的孩子。 I suppose it's better to use some kind of recursion for this but I am totally stuck.我想最好为此使用某种递归,但我完全被卡住了。

Please help me to convert it into desired nested object.请帮助我将其转换为所需的嵌套 object。

Any suggestions on other ways to sort flat array are welcome too?也欢迎任何有关对平面数组进行排序的其他方法的建议? Maybe there's a better way to sort it without actually converting back and force?也许有更好的方法来排序它而无需实际转换回来并强制?

Thanks a lot in advance!提前非常感谢!

You could sort the data directly and build a tree with uniqueId and a parent value.您可以直接对数据进行排序并使用uniqueId和父值构建树。

 const input = [{ id: 1, uniqueId: '1', name: 'folder1', dir: true }, { id: 2, uniqueId: '1.2', name: 'test1', dir: false }, { id: 3, uniqueId: '1.2.3', name: 'test2', dir: false }, { id: 4, uniqueId: '1.2.4', name: 'test3', dir: true }, { id: 3, uniqueId: '1.3', name: 'test23', dir: true }, { id: 1, uniqueId: '1.3.1', name: 'test6', dir: true }], tree = function (data) { var t = {}; data.forEach(o => { const parent = o.uniqueId.split('.').slice(0, -1).join('.'); Object.assign(t[o.uniqueId] = t[o.uniqueId] || {}, o); t[parent] = t[parent] || {}; t[parent].children = t[parent].children || []; t[parent].children.push(t[o.uniqueId]); }); return t[''].children; }(input.sort((a, b) => b.dir - a.dir)); console.log(tree);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM