【问题标题】:react native/JS nested array detect反应原生/JS嵌套数组检测
【发布时间】:2018-12-17 05:44:30
【问题描述】:

我在这里有一个嵌套数组,我在问题 300 下有一些答案。 这里有些答案也有回复。喜欢“parent_answer_id”

在我的应用程序中,我试图仅在 "parent_answer_id" == 0 显示在列表中并且如果其他项目的 "parent_answer_id" 与其他项目匹配 "id" 然后将在该循环上显示额外的内容 "more reply >"。

到目前为止,我尝试过 react native:

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",
        },

    ]
],

【问题讨论】:

    标签: javascript arrays json reactjs react-native


    【解决方案1】:
    1. 第一个答案是answers[0][0] 问题已编辑
    2. 我会先收集“主要”答案和相关答案,然后再进行演示。

    这涉及递归地为每个孩子的答案寻找(并可能创建)相同的树结构;就像您现在的架构一样,每个“顶级”答案都有一个任意嵌套的子答案层次结构,就像任何线程对话机制一样。

    【讨论】:

    • 好吧,我错了。数组上有另一个内容。让我更新一下我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2023-04-01
    • 2018-07-04
    • 1970-01-01
    相关资源
    最近更新 更多