append to json object javascript in one line code example
Example 1: how to add element in json object
object["property"] = value;
object.property = value;
var myJson = { name: "mamad", family: "mirzaei" }
myJson.age = 26
console.log( "myJson >>:" ,myJson)
{ name: "Mamad", family: "Mirzaei", age : 26 }
Example 2: 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'});