Question

I'm getting duplicates in an array I'm building in a for loop in <cfscript> tags. What could be the cause of this?

I'm trying to build an array to later parse into JSON, using a query from the database. I've checked, and the result query object doesn't contain duplicate rows; it has two individual rows, although in the array resulting of the for loop, I only get the first row twice.

l.response.body.result = ArrayNew(1);
for(l.i=1;l.i lte l.events.recordcount;l.i = (l.i + 1)){
  ArrayAppend(l.response.body["result"], StructNew());
  l.eventIndex = ArrayLen(l.response.body["result"]);
  l.response.body["result"][l.eventIndex]["id"] = l.events.id;
  l.response.body["result"][l.eventIndex]["start"] = DateDiff("s","1/1/1970",l.events.startdate) * 1000;
  l.response.body["result"][l.eventIndex]["end"] = DateDiff("s","1/1/1970",l.events.enddate) * 1000;
  l.response.body["result"][l.eventIndex]["title"] = l.events.title;
  l.response.body["result"][l.eventIndex]["class"] = "class";
  l.response.body["result"][l.eventIndex]["url"] = l.url;
}

Thanks for any help.

Was it helpful?

Solution

Never mind this, I was being extremely stupid :) I was confusing cfloop with for loops, thinking it would only take one row of the query :) I changed it to this:

l.response.body.result = ArrayNew(1);
for(l.i=1;l.i lte l.events.recordcount;l.i = (l.i + 1)){
  ArrayAppend(l.response.body["result"], StructNew());
  l.eventIndex = ArrayLen(l.response.body["result"]);
  l.response.body["result"][l.eventIndex]["id"] = l.events.id[l.i];
  l.response.body["result"][l.eventIndex]["start"] = DateDiff("s","1/1/1970",l.events.startdate[l.i]) * 1000;
  l.response.body["result"][l.eventIndex]["end"] = DateDiff("s","1/1/1970",l.events.enddate[l.i]) * 1000;
  l.response.body["result"][l.eventIndex]["title"] = l.events.title[l.i];
  l.response.body["result"][l.eventIndex]["class"] = "class";
  l.response.body["result"][l.eventIndex]["url"] = l.url;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top