0

I am going to create some count of object with specific names.

if i do

var obj = {}
obj.A = ['a', 'b', 'c'];

that create key A with array value

Can I do something like this:

var obj = {}
var keyName = 'A';

obj.keyName = ['a', 'b', 'c'];

And obj containe name of key A and array as value. Is it possible?

Arch
  • 517
  • 2
  • 7
  • 17

2 Answers2

1

Just do

obj[keyName] = ['a', 'b', 'c'];
rickythefox
  • 6,601
  • 6
  • 40
  • 62
1

You cannot do like this below:

var obj = {}
var keyName = 'A';

obj.keyName = ['a', 'b', 'c'];

If you want to access the keyName value, you could do like this obj[keyName] which would result in A or set obj[keyName] = ['a', 'b', 'c'];

Thalaivar
  • 23,282
  • 5
  • 60
  • 71