Here is my object literal:
var obj = {key1: value1, key2: value2};
How can I add {key3: value3} to the object?
Sommaire |
Here is my object literal:
var obj = {key1: value1, key2: value2};
How can I add {key3: value3} to the object?
There are two ways to add new properties to an object:
var obj = { key1: value1, key2: value2 };
obj.key3 = "value3";
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:
var arr = [];
var arr = new Array();
License : cc by-sa 3.0
http://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object