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.

1
  • is the data sorted? id not, how do you distinuish between of ` ou=smallUnit1` from ou=unit1 and ou=unit2? Commented Jul 25, 2018 at 8:02

2 Answers 2

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; }

Sign up to request clarification or add additional context in comments.

Comments

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);

2 Comments

hi thank you for your help. it looks like i can create only one branch. not a whole tree. or am i wrong?
No, it means you can create as much as you want, but the dependencies array you provided contains only one branch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.