0

In my Node/MongoDB backend I make a call using dot notation that looks like this:

await this.sybase.Clients.add(args.doc, args.metaData, this.app);

Now, to make this more usable, I want to be able to input dynamic variables. Specifically, where you see "Clients" I want to use a variable called "model", which I will be able to assign as needed.

I also want to be able to use a dynamic variable where you see "add" -- something like "action".

If this were a string I would just do this:

`this.sybase.${model}.${action}(args.doc, args.metaData, this.app)`

But clearly that won't work here. So how can I pass dynamic variables in a case like this?

Rey
  • 1,393
  • 1
  • 15
  • 24

1 Answers1

1

You can use bracket notation:

let someObj = {foo: 1, bar: 2, baz: 3};
let key = "bar";
console.log(someObj[key]);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

DaCurse
  • 795
  • 8
  • 25