简体   繁体   English

如何遍历树状未知深度的嵌套数据结构以查找和收集可寻址数组项?

[英]How to traverse through a tree like nested data structure of unknown depth in order to find and collect addressable array items?

Say I have an array that looks as such:假设我有一个看起来像这样的数组:

[{
  "name": "Audiograms",
  "folders": [{
    "name": "2022"
  }, {
    "name": "2021"
  }, {
    "name": "2020"
  }]
}, {
  "name": "Patient Paperwork"
}, {
  "name": "Repairs"
}]

And this array can have an infinite amount of objects and sub-objects, similar to a file tree.而且这个数组可以有无限数量的对象和子对象,类似于文件树。

I have an array letting me know the name of the folders I need to access from the root of the object, like:我有一个数组让我知道我需要从对象的根目录访问的文件夹的名称,例如:

["Audiograms", "2022"]

I also do not know this value ahead of time, nor do I know how many items are in this array ahead of time.我也不提前知道这个值,也不知道这个数组中有多少项目。

How would I be able to actually traverse this file tree using the array of names?我如何能够使用名称数组实际遍历此文件树? I wish to do things like maybe pop the matching object out and move it to another part of the file tree.我希望做一些事情,比如将匹配的对象弹出并将其移动到文件树的另一部分。

Thank you!谢谢!

OP OP

"I wish to do things like maybe pop the matching object out and move it to another part of the file tree." “我希望做一些事情,比如将匹配的对象弹出并将其移动到文件树的另一部分。”

In order to achieve follow-up tasks like the above mentioned one, the next provided solution walks the OP's folder structure and collects for each addressable match an object of two references, target and parent , where the former is the reference of the to be found folder-item, and the latter is the reference of its parent folder-item.为了实现上面提到的后续任务,下一个提供的解决方案遍历OP 的文件夹结构,并为每个可寻址匹配收集两个引用的对象targetparent ,其中前者是要找到的引用文件夹项,后者是其父文件夹项的引用。

The solution got achieved by a recursively implemented reduce r function.该解决方案通过递归实现的reduce r 函数实现。

 function collectAddressableFolderRecursively(collector, folderItem) { const { name = null, folders = [] } = folderItem; const { address: [parentName, childName], result, } = collector; if (name === parentName && folders.length) { const targetFolder = folders .find(({ name }) => name === childName) ?? null; if (targetFolder !== null) { result.push({ target: targetFolder, parent: folderItem, }); } } result.push( ...folders.reduce(collectAddressableFolderRecursively, { address: [parentName, childName], result: [], }) .result ); return collector; } const folders = [{ name: 'Audiograms', folders: [{ name: '2022', folders: [{ name: 'Audiograms', folders: [{ name: '2022', }, { name: 'foo', }], }], }, { name: '2021', }, { name: '2020', }] }, { name: 'Patient Paperwork', }, { name: 'Repairs', folders: [{ name: 'Audiograms', folders: [{ name: '2022', }, { name: 'bar', }], }, { name: 'baz', }], }] const address = ['Audiograms', '2022']; const { result } = folders .reduce(collectAddressableFolderRecursively, { address, result: [], }); console.log({ address, result });
 .as-console-wrapper { min-height: 100%!important; top: 0; }

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

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