Add new object in JSON array

Viewed 404

I'm working with javascript and JSON. I have a JSON object like this one:

{"firstName":"John", "lastName":"Doe", "id":"1"}

In my case, I have a function like this one:

function addNewValues(jsonArray, firstName, lastName, id)

In case the "firstName" is not a match, I should create a new value in the array.

From

addNewValues({"firstName":"John", "lastName":"Doe", "id":"1"}, "Jane", 
"Doe", "2"); 

it should return:

[
{"firstName":"John", "lastName":"Doe", "id":"1"},
{"firstName":"Jane", "lastName":"Doe", "id":"2"}
]

How can I add this new position? I tried myObj.push({"firstName":firstName, "lastName":lastName }); without any success.

Thanks in advance.

8 Answers
Related