[英]Count number of all specific keys (or values) in json object/array- JavaScript/Angular?
[英]number of xml attributes->javascript multi array->function / process
我在相对于每个帖子的数据库中有评论。 它将在一个查询中根据该帖子拉出该帖子和所有评论,并将它们分组到一个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
依此类推,评论会增加该数字。
因此,我需要检查(完成)了多少条注释,然后从xml节点中检索它们(使用$(this).attr('comment'+i)
),其中我将是计数器( 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
}
然后我将如何处理函数中的每个注释? 即:在每个评论中,执行此操作。
提前致谢!
您需要先创建内部数组,然后再添加内部数组。 尝试这个:
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);
}
//}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.