0

Assume I have the following namespaces

window.myNameSpace.1.someName

window.myNameSpace.2.someName

Also, assume that I have a runtime variable x, where x could be either 1 or 2

let x = 1; // x is 1 in this case, but could be 2

Now, based on the value of x, I could do this

switch (x) {
    
    case 1:
        let someName = window.myNameSpace.1.someName;
        break;

    case 2
        let someName = window.myNameSpace.2.someName;
        break;
}

This works and is great.

However, can I do something like this to make code more concise? (please note this is a pseudo code and not correct, this is the idea)

let someName = window.myNameSpace.<x>.someName; // replace <x> with the variable

And thus making the code much shorter and more efficient, especially if I have a lot of values for x and a number of namespaces.

Thanks.

Greeso
  • 7,544
  • 9
  • 51
  • 77

0 Answers0