-1

I have static something like code below.

let str='4'
let tmp=['A', 'B']

I want it to be like that

let myCode=4:['A', 'B']
Aneat Tea
  • 95
  • 6

1 Answers1

1

What you want is invalid JavaScript. If you want this result:

myCode = {
    4: ['A', 'B']
};

Then just do this:

let myCode = { [str]: tmp };

Or:

let myCode = {};
myCode[str] = tmp;
Ram
  • 143,282
  • 16
  • 168
  • 197
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • `str` should be `[str]`. The latter syntax is called "Computed property name". – Ram Sep 26 '18 at 02:29
  • OK, I'll try that and see what happens. Go ahead and edit with correct syntax. It does seems to work after I've tested it however @undefined – Jack Bashford Sep 26 '18 at 02:29
  • Both syntaxes are valid but do different things. The `str` creates `'str'` key and `[str]` creates a key by using `str` variable's value. – Ram Sep 26 '18 at 02:37
  • It works fine, Thank you​ for your refly – Aneat Tea Sep 26 '18 at 02:56