我在数据库中对每一篇文章都有评论。它在一个查询中根据该帖子提取post和所有注释,并将它们分组到XML节点中。我获取每个节点中的属性数量,并删除默认情况下每个帖子拥有的标准属性数,这就留下了注释的数量。
评论结构如下:
comment0 Hey nice post!
commentdate0 2014-12-1 08:25:02
commentaudthor0 Chris
comment1 cool!
commentdate1 2014-08-2 09:25:02
commentaudthor1 Jason依此类推,评论增加了这个数字。
因此,我需要检查有多少注释(已完成),然后从comment0, comment1节点(使用comment0, comment1)检索它们,其中我将是计数器(comment0, comment1,等等)。
下面是我将其放入数组的当前代码:
var comms = new Array();
var count = this.attributes.length;
var av = count-11;
if(av != 0) {
for(var i=0; i<av; i++) {
for(var j=0; j<2; j++){
comms[i][j] = $(this).attr('comment'+i);
comms[i][j+1] = $(this).attr('commentdate'+i);
comms[i][j+2] = $(this).attr('commentauthor'+i);
}
}
}但它给了我以下错误:
Uncaught TypeError: Cannot set property '0' of undefined 现在,我如何将其加载到多维数组中以存储数据,将其传递给函数,然后分别处理每一行?
这就是我想要做的:
Array {
'comment1':
comment
commentdate
commentauthor
'comment2':
comment
commentdate
commentauthor
}然后我将如何处理函数中的每个注释?每一条评论都要这样做。
提前感谢!
发布于 2014-02-20 13:40:52
在添加到内部数组之前,需要创建内部数组。试试这个:
var comms = new Array();
var count = this.attributes.length;
var av = count-11;
//if(av != 0) { // I commented this condition out, as it is not needed here
for(var i=0; i<av; i++) {
comms[i] = []; // Create a new array here before adding to it (this syntax is more common than the longer "new Array()" syntax you used
comms[i][0] = $(this).attr('comment'+i);
comms[i][1] = $(this).attr('commentdate'+i);
comms[i][2] = $(this).attr('commentauthor'+i);
}
//}https://stackoverflow.com/questions/21909250
复制相似问题