Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to add new property to 'myObj', name it 'string1' and give it a value of 'string2', but when I do it it returns 'undefined:

var myObj = new Object;
var a = 'string1';
var b = 'string2';
myObj.a = b;

alert(myObj.string1); //returns 'undefined'
alert(myObj.a); //returns 'string2'

In other words: How to create an object property and give it the name stored in the variable but not the name of the variable itself? Thanks.

share|improve this question

7 Answers 7

up vote 136 down vote accepted

There's the dot notation and the array notation

myObj[a] = b;
share|improve this answer
4  
They're not really "equivalent"; myObj.a = b means something different to myObj[a] = b (unless a == 'a') but whatever... this is what you want. –  Dominic Cooney Feb 11 '10 at 2:44
10  
What I meant is that they're equivalent ways to set a property. Obviously they behave differently (hence the purpose of posting this answer) - but they have the same result. –  philfreo Feb 11 '10 at 2:46
2  
philfreo: except that they don't have the same result, which is the basis for this question. –  bukzor Aug 24 '11 at 22:00
10  
I was just saying there are two ways to set an object, one for when the key is hard coded and one when it's variable. I meant the outcome of the object is equivalent regardless if you use myObj['foo'] = 'bar' or myObj.foo = 'bar' –  philfreo Aug 25 '11 at 3:46

Dot notation and the properties are equivalent. So you would accomplish like so:

var myObj = new Object;
var a = 'string1';
myObj[a] = 'whatever';
alert(myObj.string1)

(alerts "whatever")

share|improve this answer
    
+1 Missed it by 2 minutes! Considering how detailed you were in your answer, I imagine you may be thinking "I could've shaved off two minutes in my explanation and those 100 extra upvotes would be mine!" :-) –  Jeremy Moritz Aug 1 at 16:56

Ecu, if you do myObj.a, then it looks for the property named a of myObj. If you do myObj[a] =b then it looks for the a.valueOf() property of myObj.

share|improve this answer

You could just use this:

function createObject(propName, propValue){
    this[propName] = propValue;
}
var myObj1 = new createObject('string1','string2');

Anything you pass as the first parameter will be the property name, and the second parameter is the property value.

share|improve this answer

ES6 introduces computed property names, which allow you to do

var myObj = {[a]: b};

Note browser support is currently negligible.

share|improve this answer

Oneliner:

obj = (function(attr, val){ var a = {}; a[attr]=val; return a; })('hash', 5);

Or:

attr = 'hash';
val = 5;
var obj = (obj={}, obj[attr]=val, obj);

Anything more shorter?

share|improve this answer
1  
I'm not sure if you can count that as a one-liner, as the function inside has three statements, not one (which is usually the requirement of being a one-liner). –  Andrew Larsson Apr 27 at 2:50
    

You cannot use a variable to access a property via dot notation, instead use the array notation.

var obj= {
     'name' : 'jroi'
};
var a = 'name';
alert(obj.a); //will not work
alert(obj[a]); //should work and alert jroi'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.