像是闭包 但是不知道怎么写?

穿梭框功能 arr1是穿梭框左侧的全部数据
大致意思是 最后一层的title全部选中就把上一层的title名称放入数组中
如果子children没全部选中则把选中的title放入数组中

    arr1 = [
        {
            title:'全部',
            children:[
                {
                    title:'小明0.1',
                    children:[
                        {
                            title:'小明0.1.1',
                            children:[
                                {
                                    title:'小明0.1.1.1',
                                    children: []
                                }
                            ]
                        },
                        {
                            title:'小明0.1.2',
                            children:[
                                {
                                    title:'小明0.1.2.1',
                                    children: []
                                }
                            ]
                        },
                    ]
                },
                {
                    title:'小明0.2',
                    children:[
                        {
                          title:'小明0.2.1',
                        },
                        {
                          title:'小明0.2.2',
                        },
                        {
                          title:'小明0.2.2',
                        }
                    ]
                }
            ]
        }

    ]
阅读 381
avatarAI BotBETA

以下是回答:

arr1 = [
    {
        title:'全部',
        children:[
            {
                title:'小明0.1',
                children:[
                    {
                        title:'小明0.1.1',
                        children: []
                    },
                    {
                        title:'小明0.1.2',
                        children:[
                            {
                                title:'小明0.1.2.1',
                                children: []
                            }
                        ]
                    },
                ]
            },
            {
                title:'小明0.2',
                children:[
                    {
                      title:'小明0.2.1',
                    },
                    {
                      title:'小明0.2.2',
                    },
                    {
                      title:'小明0.2.3',
                    }
                ]
            }
        ]
    }
]
1 个回答

这听上去就是一个递归,然后修改自己的状态。

然后我看了一下 ai 的答案,不忍直视。

这里遍历的话可以考虑深度优先遍历

image.png

看上去遍历没问题

image.png

遍历的时候做两个状态判断一下就可以了


但是我没有从上往下覆盖,所以会出现这种情况

image.png

到此就完成了你所需要的功能,不过这个需求一般 tree 组件都支持


可以在计算的时候,用计算值计算,可以直接向上继承

image.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题