How to use function parameter as attribute in dotted notation with object

I have a function with a parameter which is a string. I want to use this string as an attribute on an object. Here's an example of how it would look:

var x = "somestring"
function foo(attribute) {  
    someobj.attribute = "something";
}
foo(x);

      

+3


source to share


1 answer


Use index mark []

for dynamic keys:

someobj[attribute] = "something";

      



CODE

var x = "somestring";
var someobj = {};

function foo(attribute) {
    someobj[attribute] = "something";
}
foo(x);

      

+3


source







All Articles