0

I am very new to JavaScript and read some questions about the topic but none of them gave me a solution to my problem.

I want to change user data in Active Directory, so I use ldapjs. There is a method to change a property of a user.

   client.modify(
          user['dn'],
          [new ldap.Change({
              operation: 'replace',
              modification: {
                _var_: u['st']
              }})]);

When I know the key (_var_)I want to set, this works perfectly. But I got an object that contains any to be changed key-value pairs. Something like u = { sn: 'test', l: 'test', st: 'test'}; Now I want to iterate over all properties of u and modify the related properties of user (in AD). Is there a way to set the _var_ key in modification dynamically to fit the actual key of u (sn, l, st)?

2
  • what does _var_ contain in this case? Commented Jan 31, 2018 at 13:26
  • @zola var should be 'sn' in the first iteration 'l' in the second, ... Commented Jan 31, 2018 at 13:29

1 Answer 1

1

Is this what you're looking for ? Provided that _var_ contains a string.

var modification = {};
modification[_var_]=u['st'];
client.modify(
      user['dn'],
      [new ldap.Change({
          operation: 'replace',
          modification: modification})]);

So using your u object:

for(var k in u){
    modification[k]=u[k];
}

See this fiddle.

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

7 Comments

why do I use 'modification: modification'? wouldn't it work with just 'modification'?
The first 'modification' is the key in your object, the second is the variable you created 2 lines above.
wouldn't it just try to change a field called 'modification' in AD-user?
As far as I understand the changeJS API, no.
wouldn't it work with just 'modification'?- it would depending on your nodejs version. It's new ES6 shorthand syntax. E.g.: let a = 5; let b = {a};, b is {a:5} now. Works fine on my nodejs 7.10.1
|

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.