Adding JavaScript to an object using an array key / value pair

I have an object that I built dynamically:

obj = {};
obj.prop1 = 'something';
obj.prop2 = 'something';
obj.prop3 = 'something';

With this, I now need to take an element from the array and use it to determine both the equivalent of "propX" and its value

I thought that if I did something like

obj.[arr[0]] = some_value;

What, this will work for me. But I also realized that the error I get is a syntax error. "Missing name after statement." Which I understand, but I'm not sure how to get around this. The ultimate goal is to use the value of the array element as the name of the property for the object, and then define this property with another variable to be passed. My question is how can I achieve this, so the appendage to the object will be considered as

obj.array_value = some_variable;
+5
3

.

obj[arr[0]] = some_value;

MDN.

+8

obj[arr[0]] = some_value;

. :)

+3

You are almost right, but you just need to delete. from line:

obj. [arr [0]] = some_value;

must read

obj [arr [0]] = some_value;

+3
source

All Articles