2

我在<cfscript>标签中的 for 循环中构建的数组中有重复项。这可能是什么原因?

我正在尝试使用来自数据库的查询构建一个数组,以便稍后解析为 JSON。我查过了,结果查询对象不包含重复行;它有两个单独的行,尽管在 for 循环的结果数组中,我只得到了第一行两次。

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;
}

谢谢你的帮助。

4

1 回答 1

0

没关系,我非常愚蠢:)我cfloopfor循环感到困惑,认为它只需要一行查询:)我将其更改为:

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;
}
于 2013-08-27T13:14:39.513 回答