我在这里有一个嵌套数组,在问题300下我有一些答案。 这里有些答案也有答复。像“ parent_answer_id”
在我的应用程序中,我只尝试显示“ parent_answer_id” == 0 ,并且其他项目的“ parent_answer_id”与其他项目匹配 “ id”,然后在该循环上显示额外的内容“ 更多回复> ”。
到目前为止,我尝试在本机中做出反应:
var k =0;
for ( let j = 0; j < Object.keys(item.answers[1]).length; j++) {
if (item.answers[1][j].parent_answer_id==0) {
var parent = 1;
};
if (parent) {
k++;
if (count-1 == j) {
var last = 1;
}
parents.push(
<View key={j}>
<View style={styles.answerItem}>
<View >
<Text>
{item.answers[1][j].content}
</Text>
</View>
<View>
<Text>more reply > </Text>
</View>
</View>
</View>
);
};
}
我的样本数组在这里
"answers": [
[ 5,
{
"id": "215",
"parent_answer_id": "214",
"question_id": "300",
"content": "zksjbviul",
"question_content": "gggg",
"is_like": "0",
},
{
"id": "214",
"parent_answer_id": "213",
"question_id": "300",
"content": "ksdgzbvjzh,",
"question_content": "gggg",
},
{
"id": "213",
"parent_answer_id": "0",
"question_id": "300",
"content": "kuqebvjzd",
"question_content": "gggg",
},
{
"id": "212",
"parent_answer_id": "0",
"question_id": "300",
"content": "iauvhiuds",
"question_content": "gggg",
},
{
"id": "211",
"parent_answer_id": "0",
"question_id": "300",
"content": "fdfs",
"question_content": "gggg",
},
]
],
答案 0 :(得分:1)
answers[0][0]
。这涉及为每个子答案递归查找(并可能创建)相同的树结构;就像现在的架构一样,每个“顶级”答案都有子嵌套的任意嵌套层次结构,就像任何线程对话机制一样。