views:

4311

answers:

7

Here is my object literal:

var obj = {key1: value1, key2: value2};

How can I add {key3: value3} to the object?

+5  A: 

You could use either of these (provided key3 is the acutal key you want to use)

arr[ 'key3' ] = value3;

or

arr.key3 = value3;

If key3 is a variable, then you should do:

var key3 = 'a_key';
arr[ key3 ] = value3;

In this case, arr.key3=value3 would not work like you expect.

seth
missing semicolon... ccccc...
Robert Koritnik
@Robert - thanks. ccccc?
seth
Shouldn't key3 be in quotation marks? Or am I not understanding the intention of the code?
Nosredna
+1. Thanks Seth!
James Skidmore
I thought key3 was a var when I first read it. perhaps I'm mis-undertanding it. Edited post to reflect that.
seth
@James glad I could help in my little way.
seth
A: 
arr.push({key3: value3});
dfa
+21  A: 

That's not an array. It is an object constructed using object literal notation and there are two ways to add new properties to such an object:

var obj = {
    key1: value1,
    key2: value2
};

Using dot notation:

obj.key3 = "value3";

Using square bracket notation:

obj["key3"] = "value3";

The first form is used when you know the name of the property. The second form is used when the name of the property is dynamically determined. Like in this example:

var getProperty = function (propertyName) {
    return obj[propertyName];
};

getProperty("key1");
getProperty("key2");
getProperty("key3");

A real JavaScript array can be constructed using either:

The Array literal notation:

var arr = [];

The Array constructor notation:

var arr = new Array();
Ionuț G. Stan
Thanks for the thorough explanation! I really appreciate it. The square bracket notation was exactly what I needed.
James Skidmore
Shouldn't key3 be in quotation marks?
Nosredna
No problem. Glad to help you.
Ionuț G. Stan
Yes Nosredna, you're right. I corrected after your comment on seth's answer. I know the implications, it was just the inertia of my mind :)
Ionuț G. Stan
obj is an object. The part between (and including) the braces is the object literal. obj is not an object literal.
Nosredna
Thanks for the fix, Ionut--upvoting your answer.
Nosredna
Thanks Nosredna, fixed the second issue... I think.
Ionuț G. Stan
yeah, it seems correct now.
Nosredna
+1  A: 
arr.key3 = value3;

because your arr is not really an array... It's a prototype object. The real array would be:

var arr = [{key1: value1}, {key2: value2}];

but it's still not right. It should actually be:

var arr = [{key: key1, value: value1}, {key: key2, value: value2}];
Robert Koritnik
Thanks for the explanation Robert. +1
James Skidmore
Always glad to be of some use... ;)
Robert Koritnik
+1  A: 

Your example shows an Object, not an Array. In that case, the preferred way to add a field to an Object is to just assign to it, like so:

arr.key3 = value3;
bdukes
A: 

You can either add it this way:

arr['key3'] = value3;

or this way:

arr.key3 = value3;

The answers suggesting keying into the object with the variable key3 would only work if the value of key3 was 'key3'.

wombleton
A: 

arr.push({key3: value3}); doesn't work :)