4 Replies Latest reply on Mar 23, 2009 4:30 PM by flipone01

    wierd array problem

    pranav_m Level 1

      im trying to parse an XML document into into objects, and place those objects into an Array of objects. each object has 2 String properties- a URL and a Title. The most obvious method to do this is to run a for loop on the XML doc and place a temporary object in an array after each iteration. but strangely, when the property of the- temporary object(which is getting placed in the array) is updated in the next iteration, the corresponding property in the array of the previous object also gets updated. thus in the end im getting an array of identical objects!!
      why is this happening, and how can i make it stop? Please help.
        • 1. Re: wierd array problem
          TheDude7563 Level 1
          It would help if you would post the code your using, because there can be a number of things causing such behavior.
          • 2. wierd array problem
            buabco Level 1
            Did you try to use the clone() method on the XML when copying it into the array? It sounds to me that you are copying a reference to the XML object and the updating it's properties.
            • 3. Re: wierd array problem
              Hi pranav_m and others,

              I'm getting the same problem as you did. Have you found a solution to this issue?

              Here's the code that causes me so much hair-pulling:

              var rowTemp = {id: 0, oppId: 0, paybackValue: 0, paybackPaidDate: '', paybackIsReceived: 0};
              var rows = dsPaybacksEdit.getData();
              var rowCount = rows.length;
              var rowTempArray = [];

              for (var i = 0; i < rowCount; i++)
              rowTemp["id"] = rows ["id"];
              rowTemp["oppId"] = rows
              rowTemp["paybackValue"] = Spry.$("paybacks_value_"+i).value;
              rowTemp["paybackPaidDate"] = Spry.$("paybacks_paid_date_"+i).value;
              paybackIsReceived = Utils.trim(Spry.$("paybacks_is_received_"+i).value).toLowerCase();
              rowTemp["paybackIsReceived"] = paybackIsReceived == '' ? 0 : (paybackIsReceived == "yes" ? 1 : (paybackIsReceived == "no" ? 0 : paybackIsReceived));

              //debug code here which shows the properties and their values are valid

              // add the object to the array

              //debug code here which shows the object is added to the array

              // debug code to output the content of the array of objects:
              // HERE'S WHERE THE BUG SHOWS UP
              for (var j = 0; j < rowTempArray.length; j++) {
              air.trace("j: ", j);
              air.trace('rowTempArray['+j+']["id"]', rowTempArray[j]["id"]);
              air.trace('rowTempArray['+j+']["oppId"]', rowTempArray[j]["oppId"]);
              air.trace('rowTempArray['+j+']["paybackValue"]', rowTempArray[j]["paybackValue"]);
              air.trace('rowTempArray['+j+']["paybackPaidDate"]', rowTempArray[j]["paybackPaidDate"]);
              air.trace('rowTempArray['+j+']["paybackIsReceived"]', rowTempArray[j]["paybackIsReceived"]);

              All the output rows are identical to each other, which are the values of the properties of the LAST OBJECT added to the array. I removed the debug code for the main loop, but it does show that the objects (and their properties) are being added correctly to the array.

              I'd really appreciate any input on this.

              • 4. Re: wierd array problem
                flipone01 Level 1
                I did find the solution to the bug. It turns out I can't reuse the object rowTemp by assigning values to its properties and then adding it to rowTempArray. Oh well, here's the code that works:

                var rows = dsPaybacksEdit.getData();
                var rowCount = rows.length;
                var rowTempArray = [];
                var pbPaybackId = null;
                var pbOppId = null;
                var pbPaybackValue = null;
                var pbPaybackPaidDate = null;
                var pbPaybackIsReceived = 0;

                for (var i = 0; i < rowCount; i++) {
                pbPaybackId = rows ["id"];
                pbOppId = rows
                pbPaybackValue = Spry.$("paybacks_value_"+i).value;
                pbPaybackPaidDate = Spry.$("paybacks_paid_date_"+i).value;
                pbPaybackIsReceived = Clever.Utils.trim(Spry.$("paybacks_is_received_"+i).value).toLowerCase();
                pbPaybackIsReceived = pbPaybackIsReceived == '' ? 0 : (pbPaybackIsReceived == "yes" ? 1 : (pbPaybackIsReceived == "no" ? 0 : pbPaybackIsReceived));

                rowTempArray.push({id: pbPaybackId, oppId: pbOppId, paybackValue: pbPaybackValue, paybackPaidDate: pbPaybackPaidDate, paybackIsReceived: pbPaybackIsReceived});