我希望我足够清楚。
我的数据集已填充
comment:CommentVO;
comment.replies=[comment:CommentVO,comment:CommentVO,comment:CommentVO];
_comments:Array = [comment:CommentVO,comment:CommentVO,comment:CommentVO]
我在我的CommentVO Obj中正确填充了我的 _comments 数组和回复数组。
这有效
for each (var comment : CommentVO in _comments) {
trace("TOP COMMENT" + comment.raw_message);
for each (var comment : CommentVO in comment.replies){
trace("1st DEPTH COMMENT REPLY " + comment.raw_message);
for each (var comment : CommentVO in comment.replies){
trace("2nd DEPTH COMMENT REPLY " + comment.raw_message );
for each (var comment : CommentVO in comment.replies){
trace("3rd DEPTH COMMENT REPLY " + comment.raw_message);
}
}
}
}
但我知道评论回复深度是3级。 如何进行循环检查以查看comment.replies数组是否包含内容并运行循环? 它可能看起来像这样,但这不正确。
var i:int=0;
for each (var comment : CommentVO in _comments) {
trace("TOP COMMENT" + comment.raw_message );
for each (var comment : CommentVO in comment.replies) {
trace( i + "DEPTH COMMENT REPLY " + comment.raw_message );
if(comment.replies.length >1){
i++;
}
}
}
我希望我足够清楚......我一直在用这个来敲我的头。
感谢。
答案 0 :(得分:3)
如果我理解正确,每个评论可以有0到n个回复,这些回复也可以有0到n个回复,等等。
因此,您需要为每条评论调用一个递归函数,无论其相对于原始发布的深度如何,都会跟踪该深度。
看起来应该是这样的:
function findReplies ( comment:commentVO, depth:int = 0) : String {
var output:String = formatComment (comment, depth);
for each ( var reply : commentVO in comment.replies ) {
output += findReplies ( reply, depth + 1);
}
return output;
}
您不需要使用计数器变量;只有在数组有内容时才会执行for each
循环。您可以在顶级注释上调用此方法,如果您愿意,可以一次跟踪整个树,而不是在函数体内进行跟踪调用 - 如下所示:
trace ( findReplies (topComment) );
实施formatComment()
以使输出格式良好。例如:
function formatComment ( comment:commentVO, depth:int ) : String {
var output:String = depth > 0 ? " " : "- ";
var i:int = -1;
while (++i < depth) output+= " ";
output += "Comment ";
if ( depth > 0 ) output += "reply ";
output += "at depth "+depth+":";
output += comment.raw_message;
output += "\n";
return output;
}
将返回:
- Comment at depth 0: {text}
Comment reply at depth 1: {text}
Comment reply at depth 2: {text}
Comment reply at depth 1: {text}
Comment reply at depth 1: {text}
Comment reply at depth 1: {text}
Comment reply at depth 2: {text}
Comment reply at depth 3: {text}
Comment reply at depth 3: {text}
答案 1 :(得分:0)
我写了一个测试示例,您可以参考以递归代码:
var childStructure:Array = [2,3,1,0];
function test(depth) {
for(var i = 0; i < childStructure[depth]; i++) {
test(depth+1);
}
trace("DEPTH " + (depth + 1) + " COMMENT REPLY " );
}
test(0);
以上将在控制台中生成输出,类似于:
DEPTH 4 COMMENT REPLY
DEPTH 3 COMMENT REPLY
DEPTH 4 COMMENT REPLY
DEPTH 3 COMMENT REPLY
DEPTH 4 COMMENT REPLY
DEPTH 3 COMMENT REPLY
DEPTH 2 COMMENT REPLY
DEPTH 4 COMMENT REPLY
DEPTH 3 COMMENT REPLY
DEPTH 4 COMMENT REPLY
DEPTH 3 COMMENT REPLY
DEPTH 4 COMMENT REPLY
DEPTH 3 COMMENT REPLY
DEPTH 2 COMMENT REPLY
DEPTH 1 COMMENT REPLY
答案 2 :(得分:-1)
以下是任何需要它的人的答案!如果您的数据树是动态生成的,并且您不知道子到父关系的深度,请相信我。
for each (var comment : CommentVO in _comments) {
trace("TOP COMMENT" + comment.raw_message );
listReplies(comment.replies);
}
}
private function listReplies(replies:Array):void{
for each (var comment : CommentVO in replies) {
trace( " >>>>>> " + comment.raw_message);
if (replies.length > 0 ){
listReplies( comment.replies);
}
}
}