【问题标题】:Show nested array items react native显示嵌套数组项反应原生
【发布时间】:2020-07-02 20:56:49
【问题描述】:

我有很多 cmets 及其回复如下:

[
    {
        commentId: "5efd85d5b2eff7063b8ec802",
        description: "some comment description",
        isAnonymous: false,
        createdAt: "2020-07-02T06:59:33.317Z",
        currentUserLiked: 0,
        likes: 0,
        user: {
            firstName: "ar",
            lastName: "ar",
            email: "test@email.com",
            username: "sami",
            isVerified: false,
        },
        children: [
            {
                commentId: "5efd86b7b2eff7063b8ec803",
                parentId: "5efd85d5b2eff7063b8ec802",
                description: "some comment description",
                isAnonymous: false,
                createdAt: "2020-07-02T07:03:19.405Z",
                currentUserLiked: 0,
                likes: 0,
                user: {
                    firstName: "ar",
                    lastName: "ar",
                    email: "test@email.com",
                    username: "sami",
                    isVerified: false,
                },
                children: [
                    {
                        commentId: "5efd89c4b2eff7063b8ec805",
                        parentId: "5efd86b7b2eff7063b8ec803",
                        description: "Child of Child",
                        isAnonymous: false,
                        createdAt: "2020-07-02T07:16:20.717Z",
                        currentUserLiked: 0,
                        likes: 0,
                        user: {
                            firstName: "ar",
                            lastName: "ar",
                            email: "test@email.com",
                            username: "sami",
                            isVerified: false,
                        },
                        children: [],
                    },
                ],
            },
            {
                commentId: "5efd8996b2eff7063b8ec804",
                parentId: "5efd85d5b2eff7063b8ec802",
                description: "Child of Child",
                isAnonymous: false,
                createdAt: "2020-07-02T07:15:34.341Z",
                currentUserLiked: 0,
                likes: 0,
                user: {
                    firstName: "ar",
                    lastName: "ar",
                    email: "test@email.com",
                    username: "sami",
                    isVerified: false,
                },
                children: [],
            },
        ],
    },
];

我想用 flatList 将它们显示为同一级别的所有孩子。

我该怎么做?

【问题讨论】:

    标签: javascript arrays react-native react-native-flatlist


    【解决方案1】:

    我理解正确吗?:

    const comments = [{id: 1, children: [{id: 2, children: [{id: 3, children:[]}]}]}];
    
    const flatComments = (list) => {
        return list.flatMap(el => {
            const {children, ...out} = el;
            return [out, ...flatComments(children)];
        });
    };
    
    flatComments(comments);
    
    // [{id: 1}, {id: 2}, {id: 3}];
    

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2019-05-15
      • 2017-03-18
      相关资源
      最近更新 更多