cancel
Showing results for 
Search instead for 
Did you mean: 

Working with an array of objects

srowsell
Champ in-the-making
Champ in-the-making
This is just about the stupidest problem I've seen but can't solve, but here is the code snippet:


var items;
var dailyReports=new Object();
var dailyReportsArray=new Array();

if (goodDoc)
{
   var j=0;
   items=reportList.getChildren();
   for each (child in items)
   {
      if (child.properties["reportdl:frequency"]=="Daily")
      {
         
         dailyReports.uniqueIdentifier=child.properties["reportdl:uniqueIdentifier"];
         dailyReports.suffix=child.properties["reportdl:suffix"];
         dailyReports.department=child.properties["reportdl:department"];
         dailyReports.found=false;
         dailyReportsArray.push(dailyReports);
         blurb+=j+" "+dailyReportsArray[j].uniqueIdentifier+" "+dailyReportsArray[j].suffix+" "+dailyReportsArray[j].department+" "+dailyReportsArray[j].found+"\n";  //comparison 1
         j++;
      }
      //blurb+=dailyReports.uniqueIdentifier+" "+dailyReports.suffix+" "+dailyReports.department+" "+dailyReports.found+"\n";
   }
   
   for (var i=0;i<dailyReportsArray.length;i++)
   {
      blurb+=i+" "+dailyReportsArray.uniqueIdentifier+" "+dailyReportsArray.suffix+" "+dailyReportsArray.department+" "+dailyReportsArray.found+"\n"; //comparison 2
   }


It doesn't matter where these values come from; trust me that they are being populated.  (In fact I'm grabbing them from a site data list, but that's really not important, since that's working just fine.)

Look at the assignment statement tagged "comparison 1".  As you can see, I'm assigning an int, and then each of the four properties of the new array, one at a time.  (These lines are there for debugging purposes.)  Now look at the assignment statement tagged "comparison 2".  In my mind it should look *exactly* like the first statement.  Unfortunately this isn't true; instead I get the last property values repeated for each iteration of the second loop.  For the first "blurb" I get values like this:

0 Cards v16 CDC false
1 Houses v9 Hamilton false

For the second "blurb" I get values like this:

0 Activity r22 Messages false
1 Activity r22 Messages false
2 Activity r22 Messages false

(This is repeating the last value of the array.)

Were I not already bald, I would have pulled out my hair by now.

Anyone have any thoughts?

Steve

4 REPLIES 4

anshu_kumar
Star Contributor
Star Contributor
Hello srowsell,

You should try this instead..

var items;
var dailyReports=new Object();
var dailyReportsArray=new Array();

if (goodDoc)
{
   var j=0;
   items=reportList.getChildren();
   for each (child in items)
   {
      if (child.properties["reportdl:frequency"]=="Daily")
      {
         dailyReports = {
            "uniqueIdentifier":child.properties["reportdl:uniqueIdentifier"],
            "suffix":child.properties["reportdl:suffix"],
            "department":child.properties["reportdl:department"],
            "found":false,
         };         
         dailyReportsArray.push(dailyReports);
         blurb+=j+" "+dailyReportsArray[j].uniqueIdentifier+" "+dailyReportsArray[j].suffix+" "+dailyReportsArray[j].department+" "+dailyReportsArray[j].found+"\n";  //comparison 1
         j++;
      }
      //blurb+=dailyReports.uniqueIdentifier+" "+dailyReports.suffix+" "+dailyReports.department+" "+dailyReports.found+"\n";
   }

   for (var i=0;i<dailyReportsArray.length;i++)
   {
      blurb+=i+" "+dailyReportsArray.uniqueIdentifier+" "+dailyReportsArray.suffix+" "+dailyReportsArray.department+" "+dailyReportsArray.found+"\n"; //comparison 2
   }


Let me know if this helps.

srowsell
Champ in-the-making
Champ in-the-making
Anshu…

That did the trick.  Was I just being stubborn not making it into a JSON object?  I still don't understand why the first assignment worked and the second one didn't, but I'll live with having a working piece of code.

Thanks very much for your help,

Steve

scouil
Star Contributor
Star Contributor
Hi,

OK so that's what happens:
You declare a "dailyReports" object at the beginning. and you append that object to your array. Hence your array has a pointer to that object.
And then at each loop, you just change the values of that object (not declaring a new one) and add that same object once more. etc.

. So at the end of your first loop first iteration, the array contains:
Cards v16 CDC false
. Then at the end of the second iteration it contains
Houses v9 Hamilton false
Houses v9 Hamilton false
. And at the end of the third it contains:
Activity r22 Messages false
Activity r22 Messages false
Activity r22 Messages false
Because in fact all 3 elements of your array point to the same object.

However, in anshu.kumar solution, you redeclare a new object each each loop iteration.
Keeping your old code but just adding
dailyReports = {};

As the first line of the loop would work as well. But I prefer assigning the whole object at once as anshu.kumar did.

srowsell
Champ in-the-making
Champ in-the-making
Ah, I get it now.  Javascript was never my first language, so suggestions – I was going to say "pointers", but didn't want to make a pun – are always welcome, and valuable.

I think we can mark this one solved.  Thanks again,

Steve
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.