Reputation: 59
Here is my object literal:
var obj = {key1: value1};
How can I add:
{key1: value1,value2,value3}
to the object?
Upvotes: 5
Views: 702
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
Reputation: 31428
Try
var obj = {"key": "val1"};
obj.key=[obj.key, "val2", "val3"]
Upvotes: 0
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