I'm trying to append new properties to some JSON objects not at alls. How can I do that?
For example, this is my JSON array:
var eventsArray;
eventsArray = [
{
"title" : "Marco 1",
"user": "Marco",
"start" : "2016-01-12T16:00:00-05:00",
"end" : "2016-01-12T17:00:00-05:00"
}, {
"title" : "Marco 2",
"user": "Marco",
"start" : "2016-01-12T10:00:00-05:00",
"end" : "2016-01-12T12:00:00-05:00"
}, {
"title" : "Marta 1",
"user": "Marta",
"start" : "2016-01-12T09:00:00-05:00",
"end" : "2016-01-12T10:00:00-05:00"
}, {
"title" : "Marta 2",
"user": "Marta",
"start" : "2016-01-13T09:00:00-05:00",
"end" : "2016-01-13T10:00:00-05:00"
}, {
"title" : "Veronica 1",
"user": "Veronica",
"start" : "2016-01-12T13:00:00-05:00",
"end" : "2016-01-12T14:00:00-05:00"
}, {
"title" : "Veronica 2",
"user": "Marco",
"start" : "2016-01-11T13:00:00-05:00",
"end" : "2016-01-11T14:00:00-05:00"
}
];
And then I need to add a new property, such as "color" : "red" to the user Veronica, i.e.:
var eventsArray;
eventsArray = [
{
"title" : "Marco event 1",
"user": "Marco",
"start" : "2016-01-12T16:00:00-05:00",
"end" : "2016-01-12T17:00:00-05:00"
}, {
"title" : "Marco event 2",
"user": "Marco",
"start" : "2016-01-12T10:00:00-05:00",
"end" : "2016-01-12T12:00:00-05:00"
}, {
"title" : "Marta event 1",
"user": "Marta",
"start" : "2016-01-12T09:00:00-05:00",
"end" : "2016-01-12T10:00:00-05:00"
}, {
"title" : "Marta event 2",
"user": "Marta",
"start" : "2016-01-13T09:00:00-05:00",
"end" : "2016-01-13T10:00:00-05:00"
}, {
"title" : "Veronica event 1",
"user": "Veronica",
"start" : "2016-01-12T13:00:00-05:00",
"end" : "2016-01-12T14:00:00-05:00",
"color" : "red"
}, {
"title" : "Veronica event 2",
"user": "Veronica",
"start" : "2016-01-11T13:00:00-05:00",
"end" : "2016-01-11T14:00:00-05:00",
"color" : "red"
}
];
Here above I just added (manually) the "color" : "red" only to the objects having "user" === "Veronica" (the latter two). How I can do this via JavaScript?
I tried some ways without success, such as:
eventsArray[4].color = "red";
eventsArray[5].color = "red";
or:
eventsArray.user["Veronica"].color = "red";
eventsArray.user["Veronica"].color = "red";
Is from a few days I'm learning to use JSON.
Please note that in this specific case there will be not any problems having objects with different properties: my program will not explode!
eventsArray.forEach(function (item) {if(item.title.indexOf('Veronica') > -1) item.color='red';})title, but theuser.