1

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.

Nico Schuck
  • 832
  • 3
  • 15
  • 32

2 Answers2

2

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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);
  • hi thank you for your help. it looks like i can create only one branch. not a whole tree. or am i wrong? – Nico Schuck Jul 26 '18 at 08:42
  • No, it means you can create as much as you want, but the `dependencies` array you provided contains only one branch. –  Jul 26 '18 at 08:44