简体   繁体   English

TypeScript平面数组到对象树

[英]TypeScript flat Array to object tree

I implement a UI for better Overview about our LDAP branchs. 我实现了一个UI,以更好地了解LDAP分支。 For that I want to use Angular Materials Tree. 为此,我想使用Angular Materials Tree。 It´s great and very intuitiv to click through all branches. 单击所有分支非常好,非常直观。 ( https://material.angular.io/components/tree/overview ) https://material.angular.io/components/tree/overview

What I have: 我有的:

Array of multiple LDAP paths as a String: 多个LDAP路径的数组作为字符串:

groups = [
     'cn=devops,ou=smallUnit1,ou=unit1,o=group1,c=de',
     'cn=devops,ou=smallUnit1,ou=unit1,o=group2,c=de',        
     'cn=devops,ou=smallUnit2,ou=unit1,o=group1,c=de',
     'cn=dev,ou=smallUnit1,ou=unit2,o=group1,c=de',
     'cn=dev,ou=smallUnit1,ou=unit1,o=group2,c=de',
     'cn=ops,ou=smallUnit1,ou=unit1,o=group1,c=de'
]

What I already did: 我已经做了:

I convertet this Strings to Array of standalone paths with dependencies. 我将此字符串转换为具有依赖项的独立路径数组。 example for groups[0]: 组[0]的示例:

dependencies = [
   {id: 'c=de',          parent: NULL,            child: 'o=group1'},
   {id: 'o=group1',      parent: 'c=de',          child: 'ou=unit1'},
   {id: 'ou=unit1',      parent: 'o=group1',      child: 'ou=smallUnit1'},
   {id: 'ou=smallUnit1', parent: 'ou=unit1',      child: 'cn=devops'},
   {id: 'cn=devops',     parent: 'ou=smallUnit1', child: NULL}

]

What I need: 我需要的:

I need an Object in which all keys are paths of our LDAP: 我需要一个其中所有键都是LDAP路径的对象:

{
   c=de: {
       o=group1: {
          ou=unit1: {
             ou=smallUnit1: {
                cn=devops: {},
                cn=ops: {}
             }
             ou=smallUnit2: {
                cn=devops: {}
             }
          },
          ou=unit2: {
             ou=smallUnit1: {
                cn=dev: {}
             }
          }
       },
       o=group2: {
          ou=unit1: {
             ou=smallUnit1: {
                cn=devops: {},
                cn=dev: {}
             }
          }
       }
   }
} 

I already tried to use methods like that: Build tree array from flat array in javascript But this algorithmus use push function to add the new branches to an arraykey. 我已经尝试使用这样的方法: 从javascript中的平面数组构建树数组,但是该算法使用push函数将新的分支添加到arraykey中。 I need that the key is an object with more keys. 我需要键是具有更多键的对象。

You could use directly groups , because all information in the reversed order are present, and dependencies is not. 您可以直接使用groups ,因为存在所有相反顺序的信息,而没有dependencies

Basically you need to 基本上你需要

  • iterate groups 迭代groups
  • split strings of groups 分裂串groups
  • take the values from the right side as key for an object and return for every step the inner object. 将右侧的值用作对象的键,并在每一步返回内部对象。

 var groups = ['cn=devops,ou=smallUnit1,ou=unit1,o=group1,c=de', 'cn=devops,ou=smallUnit1,ou=unit1,o=group2,c=de', 'cn=devops,ou=smallUnit2,ou=unit1,o=group1,c=de', 'cn=dev,ou=smallUnit1,ou=unit2,o=group1,c=de', 'cn=dev,ou=smallUnit1,ou=unit1,o=group2,c=de', 'cn=ops,ou=smallUnit1,ou=unit1,o=group1,c=de'], tree = groups.reduce((object, string) => { string .split(',') .reduceRight((o, k) => o[k] = o[k] || {}, object); return object; }, {}); console.log(tree); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Seems to me that this should do the trick (given your current example data) : 在我看来,这应该可以解决问题(鉴于您当前的示例数据):

 const dependencies = [ {id: 'c=de', parent: null, child: 'o=group1'}, {id: 'o=group1', parent: 'c=de', child: 'ou=unit1'}, {id: 'ou=unit1', parent: 'o=group1', child: 'ou=smallUnit1'}, {id: 'ou=smallUnit1', parent: 'ou=unit1', child: 'cn=devops'}, {id: 'cn=devops', parent: 'ou=smallUnit1', child: null} ]; let transformed = {}; let tracker = transformed; dependencies.forEach(d => { tracker[d.id] = {}; tracker = tracker[d.id]; }); console.log(transformed); 

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

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