add new value to object javascript code example
Example 1: how to add field to object in js
var obj = {
key1: "a",
key2: "b"
};
obj.key3 = "c";
obj["key3"] = "c";
console.log(obj)
Example 2: javascript add to object
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 3: adding element to javascript object
let person = {
name : 'John Doe',
age : 35
}
person.occupation = 'Web Designer'
person['occupation'] = 'Web Designer';
object[yourKey] = yourValue;
object.yourKey = yourValue;
Example 4: javascript append to object
How about storing the alerts as records in an array instead of properties of a single object ?
var alerts = [
{num : 1, app:'helloworld',message:'message'},
{num : 2, app:'helloagain',message:'another message'}
]
And then to add one, just use push:
alerts.push({num : 3, app:'helloagain_again',message:'yet another message'});