Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

javascript - Updating elemint in JSON updates ALL

I have a JSON object parsed and I am trying to navigate down to SHIPPINGCOMMENTS and update it, but when I do, it updates all cells with that name instead of just the one.

{
    "id": 1402846607011,
    "status": "unsaved",
    "accounts": [
        {
            "compid": 919759,
            "compname": null,
            "products": [
                {
                    "BCINUM": "539504",
                    "ITEMUNIT": "EA",
                    "ORDERDETAILS": [
                        {
                            "SHIPDATEID": "69230",
                            "SHIPPERIODID": "2096",
                            "QUANTITY": "1"
                        },
                        {
                            "SHIPDATEID": "69231",
                            "SHIPPERIODID": "2096",
                            "QUANTITY": "2"
                        }
                    ],
                    "SHIPPINGCOMMENTS": "sooner"
                }
            ]
        },
        {
            "compid": 920001,
            "compname": null,
            "products": [
                {
                    "BCINUM": "539504",
                    "ITEMUNIT": "EA",
                    "ORDERDETAILS": [
                        {
                            "SHIPDATEID": "69230",
                            "SHIPPERIODID": "2096",
                            "QUANTITY": "1"
                        },
                        {
                            "SHIPDATEID": "69231",
                            "SHIPPERIODID": "2096",
                            "QUANTITY": "2"
                        }
                    ],
                    "POTEXT": "",
                    "SHIPPINGCOMMENTS": "sooner"
                }
            ]
        }
    ]
}

Here is my code I am looping through it with:

function updateComments(compID,bcinum,comment) {
    var accounts = runningOrders.accounts;
    var n = accounts.length;
    for (i = 0; i < n; i++) {
        if (accounts[i].compid == compID) {
            var p = accounts[i].products.length;
            for (ii = 0; ii < p; ii++) {
                if (accounts[i].products[ii].BCINUM == bcinum) {
                    accounts[i].products[ii].SHIPPINGCOMMENTS = comment;
                }   
            }       
        }
    }
}

The function call is:

updateComments(919759,539504,sooner);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two potential issues:

  1. When calculating n, you are actually not calculating the size of the acconts array? What is the name of your (JSON) object?
  2. In your example function call, you are changing the name to the already defined name in both objects. Is this a mistake?

This code is working for me in node.js:

function updateComments(compID,bcinum,comment) {
  var n = accounts.accounts.length;
  console.log(n);
  console.log(accounts['accounts'][0].products);
  console.log(accounts['accounts'][1].products);
  for (i = 0; i < n; i++) {
    console.log('testing ', i, accounts.accounts[i].compid)
    if (accounts.accounts[i].compid == compID) {
      var p = accounts.accounts[i].products.length;
      console.log('Found compid', i, compID);
      for (ii = 0; ii < p; ii++) {
        if (accounts.accounts[i].products[ii].BCINUM == bcinum) {
          console.log('Found bcinum', ii, bcinum)
          accounts.accounts[i].products[ii].SHIPPINGCOMMENTS = comment;
        }
      }
    }
  }
  console.log(accounts['accounts'][0].products);
  console.log(accounts['accounts'][1].products);
}

accounts = {
"id": 1402846607011,
"status": "unsaved",
"accounts":
 [
    {
        "compid": 919759,
        "compname": null,
        "products": [
            {
                "BCINUM": "539504",
                "ITEMUNIT": "EA",
                "ORDERDETAILS": [
                    {
                        "SHIPDATEID": "69230",
                        "SHIPPERIODID": "2096",
                        "QUANTITY": "1"
                    },
                    {
                        "SHIPDATEID": "69231",
                        "SHIPPERIODID": "2096",
                        "QUANTITY": "2"
                    }
                ],
                "SHIPPINGCOMMENTS": "sooner"
            }
        ]
    },
    {
        "compid": 920001,
        "compname": null,
        "products": [
            {
                "BCINUM": "539504",
                "ITEMUNIT": "EA",
                "ORDERDETAILS": [
                    {
                        "SHIPDATEID": "69230",
                        "SHIPPERIODID": "2096",
                        "QUANTITY": "1"
                    },
                    {
                        "SHIPDATEID": "69231",
                        "SHIPPERIODID": "2096",
                        "QUANTITY": "2"
                    }
                ],
                "POTEXT": "",
                "SHIPPINGCOMMENTS": "sooner"
            }
        ]
    }
]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...