131

It's difficult to explain the case by words, let me give an example:

var myObj = {
    'name': 'Umut',
    'age' : 34
};

var prop = 'name';
var value = 'Onur';

myObj[name] = value; // This does not work

eval('myObj.' + name) = value;   //Bad coding ;)

How can I set a variable property with variable value in a JavaScript object?

4
  • possible duplicate of How to create object property from variable value in javascript? Commented Jun 22, 2011 at 12:37
  • 5
    Have a close look. It seems you just forgot to adjust cour code. It should be myObj[prop] = value;. eval('myObj.'+name) does not work either as the variable name does not exist. Commented Jun 22, 2011 at 12:37
  • 9
    you should really use more var keyboards for declaring variables, use more semicolons, not use eval and accept more answers. Done. Commented Jun 22, 2011 at 12:37
  • 1
    Your question is flawed -- that does work, but you made a mistake. You wrote "myObj[name]" when I'm quite sure you meant to write "myObj[prop]". Commented Jun 22, 2011 at 12:39

7 Answers 7

194
myObj[prop] = value;

That should work. You mixed up the name of the variable and its value. But indexing an object with strings to get at its properties works fine in JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

69
myObj.name=value

or

myObj['name']=value     (Quotes are required)

Both of these are interchangeable.

Edit: I'm guessing you meant myObj[prop] = value, instead of myObj[name] = value. Second syntax works fine: http://jsfiddle.net/waitinforatrain/dNjvb/1/

3 Comments

If the object property is a reserved word, the second syntax is required.
While this is correct, it doesn't really answer his question; he's wondering what to do when the property name is in a variable.
Updated answer there, you have name where you should have prop
7

You can get the property the same way as you set it.

foo = {
 bar: "value"
}

You set the value foo["bar"] = "baz";

To get the value foo["bar"]

will return "baz".

Comments

5

You could also create something that would be similar to a value object (vo);

SomeModelClassNameVO.js;

function SomeModelClassNameVO(name,id) {
    this.name = name;
    this.id = id;
}

Than you can just do;

   var someModelClassNameVO = new someModelClassNameVO('name',1);
   console.log(someModelClassNameVO.name);

Comments

4

simple as this myObj.name = value;

Comments

3

When you create an object myObj as you have, think of it more like a dictionary. In this case, it has two keys, name, and age.

You can access these dictionaries in two ways:

  • Like an array (e.g. myObj[name]); or
  • Like a property (e.g. myObj.name); do note that some properties are reserved, so the first method is preferred.

You should be able to access it as a property without any problems. However, to access it as an array, you'll need to treat the key like a string.

myObj["name"]

Otherwise, javascript will assume that name is a variable, and since you haven't created a variable called name, it won't be able to access the key you're expecting.

1 Comment

There's still a difference between myObj[name] and myObj.name though, because the former refers to a variable name and the second to a literal key.
3

You could do the following:

var currentObj = {
    name: 'Umut',
    age : 34
};

var newValues = {
    name: 'Onur',
}

Option 1:

currentObj = Object.assign(currentObj, newValues);

Option 2:

currentObj = {...currentObj, ...newValues};

Option 3:

Object.keys(newValues).forEach(key => {
    currentObj[key] = newValues[key];
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.