简体   繁体   中英

TypeScript flat Array to object tree

I implement a UI for better Overview about our LDAP branchs. For that I want to use Angular Materials Tree. It´s great and very intuitiv to click through all branches. ( https://material.angular.io/components/tree/overview )

What I have:

Array of multiple LDAP paths as a String:

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]:

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:

{
   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. 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.

Basically you need to

  • iterate groups
  • split strings of 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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