js如何实现树形数组的筛选?

发布于 2022-09-12 00:54:15 字数 2539 浏览 20 评论 0

问题描述

给定一个这样的数组arr

    var arr=[{
            "name": "Baseinfo",
            "children": [{
                "name": "Baseinfo_School",
                "children": [{
                    "name": "Baseinfo_Test"
                }]
            }, {
                "name": "Baseinfo_Teacher"
            }]
        }]

如何从这个数组test中筛选出相同的结构

 var test = [{
                path: '/baseinfo',
                //组件名
                name: 'Baseinfo',
                component: Home,
                children: [{
                        path: '/baseinfo/school',
                        name: 'Baseinfo_School',
                        component: BaseinfoSchool,
                        children: [{
                            path: '/baseinfo/school/test',
                            name: 'Baseinfo_Test',
                            component: BaseinfoSchoolTest
                        } ]
                    },
                    {
                        path: '/baseinfo/teacher',
                        name: 'Baseinfo_Teacher',
                        component: BaseinfoTeacher,
                        children: [{
                            path: '/baseinfo/teacher/test',
                            name: 'Baseinfo_Test1',
                            component: BaseinfoTeacherTest
                        } ]
                    },
                ]
            },
            {
                path: '/others',
                name: 'Others',
                component: Others
            }
        ]

得到这样的结果result,就是去除了other项和baseinfoTeacherTest项

var result  = [{
                path: '/baseinfo',
                //组件名
                name: 'Baseinfo',
                component: Home,
                children: [{
                        path: '/baseinfo/school',
                        name: 'Baseinfo_School',
                        component: BaseinfoSchool,
                        children: [{
                            path: '/baseinfo/school/test',
                            name: 'Baseinfo_Test',
                            component: BaseinfoSchoolTest
                        } ]
                    },
                    {
                        path: '/baseinfo/teacher',
                        name: 'Baseinfo_Teacher',
                        component: BaseinfoTeacher
                    },
                ]
            }
        ]

数据结构小白,那位大佬可以写个案例吗?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2022-09-19 00:54:15
  var arr = [
    {
      name: 'Baseinfo',
      children: [
        {
          name: 'Baseinfo_School',
          children: [
            {
              name: 'Baseinfo_Test',
            },
          ],
        },
        {
          name: 'Baseinfo_Teacher',
        },
      ],
    },
  ];

  var test = [
    {
      path: '/baseinfo',
      //组件名
      name: 'Baseinfo',
      component: 'Home',
      children: [
        {
          path: '/baseinfo/school',
          name: 'Baseinfo_School',
          component: 'BaseinfoSchool',
          children: [
            {
              path: '/baseinfo/school/test',
              name: 'Baseinfo_Test',
              component: 'BaseinfoSchoolTest',
            },
          ],
        },
        {
          path: '/baseinfo/teacher',
          name: 'Baseinfo_Teacher',
          component: 'BaseinfoTeacher',
          children: [
            {
              path: '/baseinfo/teacher/test',
              name: 'Baseinfo_Test1',
              component: 'BaseinfoTeacherTest',
            },
          ],
        },
      ],
    },
    {
      path: '/others',
      name: 'Others',
      component: 'Others',
    },
  ];

  function select(from, target, ret = []) {
    from.forEach((item, index) => {
      const name = item.name;
      const child = target[index];

      if (!child || child.name !== name) {
        return;
      }

      ret[index] = { ...child };

      delete ret[index].children;

      if (item.children && child.children) {
        const childClonedList = select(item.children, child.children);

        if (childClonedList.length) {
          ret[index].children = childClonedList;
        }
      }
    });

    return ret;
  }

  console.log(select(arr, test));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文