how to add element in object javascript code example
Example 1: how to add property to object in javascript
var data = {
'PropertyA': 1,
'PropertyB': 2,
'PropertyC': 3
};
data["PropertyD"] = 4;
alert(data.PropertyD);
alert(data["PropertyD"]);
Example 2: ad data to js object
var myObject = {
sProp: 'some string value',
numProp: 2,
bProp: false
};
myObject.prop2 = 'data here';
myObject.sProp = 'A new string value for our original string property';
alert(myObject.sProp)
var val = myObject.sProp;
Example 3: how to add items to object in javascript
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push({element: element});
Example 4: js add data in object
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);