0

I want to do something like this.

var abc = require('./index')
var x = "someModule";
abc.x.someFunction();

So basically I want to do something like this:

abc.someModule.someFunction();

How do I achieve this?

user3762146
  • 205
  • 3
  • 13
  • `abc[x].someFunction()` – Jared Smith Apr 19 '18 at 13:20
  • 2
    Possible duplicate of [Add a property to a JavaScript object using a variable as the name?](https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – Jared Smith Apr 19 '18 at 13:20

2 Answers2

1

Use bracket notation

Something like

var x = "someModule";
abc[x].someFunction();
Muhammad Usman
  • 10,039
  • 22
  • 39
0

This should work:

var abc = require('./index')
var x = "someModule";
abc.[x].someFunction();
Sello Mkantjwa
  • 1,798
  • 1
  • 20
  • 36