1

I have this

oArray = {};
aProperty = "Property1";
aValue = "Value1";

Is there any way I can use aProperty value as a object name in the array? This is the desired result:

oArray = {
  Property1: "Value1"
};

Thanks in advance and regards

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 4
    Use `oArray = {[aProperty]: aValue}`. This is called a [computed property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015). – CRice Aug 27 '20 at 16:33
  • It worked, thanks a mil. The IDE was complaining about the syntax but in the end it executed without problems – Majut Bamkbar Aug 27 '20 at 16:49

2 Answers2

4

It is as simple as below.

var oArray = {};
var aProperty = "Property1";
var aValue = "Value1";

oArray[aProperty] = aValue;
console.log(oArray)
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
1

Yes like this:

aProperty = "Property1";
aValue = "Value1";
oArray = {[aProperty]: aValue};
apena
  • 2,091
  • 12
  • 19