Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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"
            }
        ]
    }
]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...