user760912
user760912

Reputation: 59

Javascript Object literal

Here is my object literal:

var obj = {key1: value1};

How can I add:

{key1: value1,value2,value3}

to the object?

Upvotes: 5

Views: 702

Answers (4)

Kyle Sletten
Kyle Sletten

Reputation: 5413

If you're declaring it with the values use an array with the object literal.

var obj = {key1: [value1, value2, value3]}

If you already declared the values, still use an array, but just set the property.

obj.key1 = [value1, value2, value3]

Upvotes: 2

Jonas Elfström
Jonas Elfström

Reputation: 31428

Try

var obj = {"key": "val1"};
obj.key=[obj.key, "val2", "val3"]

Upvotes: 0

Gaurav
Gaurav

Reputation: 28755

var obj = {key1: [value1, value2, value3]};

Upvotes: 3

KARASZI István
KARASZI István

Reputation: 31467

You should modify your object at the specified key to have an array instead of a simple value:

obj.key1 = [ obj.key1, 'value2', 'value3' ];

Upvotes: 1

Related Questions