I have below JSON Array and I am trying to add new object to one of the array element, but problem that I am facing is:
If I push new object for an array array1 at index 0 of arrayHolder then all the array1 elements are getting updated with new object.
{
"arrayHolder": [
{
"array1": [],
"array2": [],
"array3": [],
"array4": []
},
{
"array1": [],
"array2": [],
"array3": [],
"array4": []
},
{
"array1": [],
"array2": [],
"array3": [],
"array4": []
},
{
"array1": [],
"array2": [],
"array3": [],
"array4": []
}
]
}
I am using below code to push to array1:
var jsonStr = "{\"arrayHolder\":[{\"array1\": [],\"....."; // Json String
var jsonObj = JSON.parse(jsonStr); // String To object
var newObj = {"value": 1}; // New object that I want to push
jsonObj.arrayHolder[0].array1.push(newObj);
// Even below code has same output
jsonObj.arrayHolder[0]["array1"][0] = newObj;
I get below output:
{
"arrayHolder": [
{
"array1": [{"value": 1}],
"array2": [],
"array3": [],
"array4": []
},
{
"array1": [{"value": 1}],
"array2": [],
"array3": [],
"array4": []
},
{
"array1": [{"value": 1}],
"array2": [],
"array3": [],
"array4": []
},
{
"array1": [{"value": 1}],
"array2": [],
"array3": [],
"array4": []
}
]
}
I want to update value of array1 only for the 0th element in the arrayHolder array not for all the array1 elements in the main array.
newObjis only added to the 0th element in the arrayHolder array.