简体   繁体   中英

JavaScript updating value of array element

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.

The issue is with the array holder, it seems that the array holder is using the same object, and so modifying it in one place will cause the change to be reflected in all places.

Try:

jsonObj.['arrayHolder'][0]['array1'].push(newObj);

Please try this Solution, @ehab has given you resion why your solution not working, you are using same object to complete object x and as you know object and array are Ref type in Javascript

 var x={ "arrayHolder": [ { "array1": [], "array2": [], "array3": [], "array4": [] }, { "array1": [], "array2": [], "array3": [], "array4": [] }, { "array1": [], "array2": [], "array3": [], "array4": [] }, { "array1": [], "array2": [], "array3": [], "array4": [] } ] } var jsonStr =JSON.stringify(x)// Json String var jsonObj = JSON.parse(jsonStr); // String To object debugger var newObj = {"value": 1}; // New object that I want to push jsonObj.arrayHolder[0].array1.push(newObj); result=JSON.stringify(jsonObj) console.log("result:"+result)

result:{"arrayHolder":[{"array1":[{"value":1}],"array2":[],"array3":[],"array4":[]},{"array1":[],"array2":[],"array3":[],"array4":[]},{"array1":[],"array2":[],"array3":[],"array4":[]},{"array1":[],"array2":[],"array3":[],"array4":[]}]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM