我有这样的结构
[
{"id": "as2is0", "replies": [{"id": "e345k2e", "replies": [. . .]} ]},
{"id": "2ishkl8"}
] . . .
很难预测评论有多少回复。我基本上想遍历每个单独的评论,无论是回复还是顶级评论。我该怎么做呢?可能还会有其他答复,因此我的基本循环解决方案不起作用。
答案 0 :(得分:3)
您想要的是一个递归函数,这意味着它在某些条件下会自行调用。这是一个例子
const data = [
{
id: "abc",
replies: [
{id: "def", replies: [
{id: 'ghi', replies: [] }
]}
]
},
{
id: "jkl",
replies: [
{id: "mno", replies: [
{id: 'pqr', replies: [] }
]}
]
}
]
function logReplies(data) {
data.forEach(item => {
console.log('id:', item.id);
if (item.replies) {
logReplies(item.replies);
}
});
}
logReplies(data);
答案 1 :(得分:1)
这是一个非常简单的recursive function,只需打印ids
即可入门:
const data = [{
id: "as2is0",
replies: [{
id: "e345k2e",
replies: [{
id: "e34112e",
replies: []
}]
}]
},
{
"id": "2ishkl8"
}
]
// notice the function accepts an array as a parameter
const getComments = arr => arr.forEach(x => { // <-- for every element
if(x.id)
console.log(x.id)
if(x.replies) // <-- if there are any children
getComments(x.replies) // <-- call with the children array as parameter now
})
getComments(data) // <-- do the initial call of the function
想法是通过在每个级别上调用getComments
来遍历树,并一直这样做,直到不再有任何子级/答复为止。
答案 2 :(得分:0)
如果您首先对检索扁平化的注释感兴趣,而不是在遍历该注释时直接进行处理(其他答案会指向您),则可以执行以下操作:
FileInputStream in = new FileInputStream("D:\\work\\calculatepi\\sampleresult.xlsx");
Workbook workbook = StreamingReader.builder()
.rowCacheSize(100)
.bufferSize(4096)
.open(in);
这还将确保所有不是const comments = [
{"id": "as2is0", "replies": [{"id": "e345k2e", "replies": []} ]},
{"id": "2ishkl8"}
];
function getFlattenedComments(comments) {
const flattenedComments = [];
for ({replies, ...commentWithoutReplies} of comments) {
flattenedComments.push(commentWithoutReplies);
if (replies) {
flattenedComments.push(...getFlattenedComments(replies));
}
}
return flattenedComments;
}
console.log(getFlattenedComments(comments));
的属性(不仅仅是replies
,如果有的话也可能包含其他属性)将保留在扁平化的集合中。
答案 3 :(得分:0)
您将需要使用递归来解决您的问题。我不确定您要对每个答复执行什么操作,或者对每个子注释需要采取什么操作,但是在下面的代码段中,我假设您想以树状结构解析它们,并使用缩进标记它们在树中水平。
当然,您可以更改解析代码并将其替换为您需要的任何操作。
// here is my attempt at building a complex layers of nested comments
var comments = [
{"id": "as2is0", "replies": [
{"id": "e345k2e", "replies": []},
{"id": "f12ba55", "replies": [{"id":"st123abc","replies":[{"id": "y345k2e", "replies": []}]}]} ]},
{"id": "k345k2e", "replies": [{"id": "q96yyt", "replies": []}]},
{"id": "2ishkl8"}
];
// begin at the outer level, parse each element separately
comments.forEach(function(c){
console.log(parse(c));
});
// the parse function takes two parameters: the comment object and the level
function parse(c,lvl) {
//if level is not specified, it is assumed to be root level
if(!lvl) { lvl = 0 }
// if current comment contains no replies, or empty array:
if(!c.replies || c.replies.length==0) {
return c.id;
} else {
// build a string to contain all nested replies
var str = c.id + " : " ;
// iterate through all replies of current comment
c.replies.forEach(function(r){
str += "\n" ; // add a new line
str += (" ").repeat( lvl+1); // indent reply as depending on the level
str += parse(r,(lvl+1)); // recursive call to parse cunction
// but with one level deeper
});
return str;
}
}