根据其值过滤JavaScript深度嵌套的对象数组

时间:2019-03-19 19:11:40

标签: javascript arrays filtering

我的菜单结构如下:

const menu = [
  {
    title: 'Supervisor Dashboard',
    link: '/dashboard/supervisor-dashboard',
    slug: '/dashboard/supervisor-dashboard'
  },
  {
    title: 'User Dashboard',
    link: '/dashboard/user-dashboard',
    slug: '/dashboard/user-dashboard'
  },
  {
    title: 'Inventory',
    slug: '/inventory',
    children: [
      {
        title: 'Add Inventory',
        link: '/inventory/add-inventory',
        slug: '/inventory/add-inventory'
      },
      {
        title: 'Remove Inventory',
        link: '/inventory/remove-inventory',
        slug: '/inventory/remove-inventory'
      },
    ]
  },
  {
    title: 'Membership',
    slug: '/membership',
    children: [
      {
        title: 'Program A',
        slug: '/membership/program-a',
        children: [
          {
            title: 'View Membership',
            link: '/membership/program-a/view',
            slug: '/membership/program-a/view'
          },
          {
            title: 'Add Membership',
            link: '/membership/program-a/add',
            slug: '/membership/program-a/add'
          },
          {
            title: 'Delete Membership',
            link: '/membership/program-a/delete',
            slug: '/membership/program-a/delete'
          }
        ]
      },
      {
        title: 'Program B',
        slug: '/membership/program-b',
        children: [
          {
            title: 'View Membership',
            link: '/membership/program-b/view',
            slug: '/membership/program-b/view'
          },
          {
            title: 'Add Membership',
            link: '/membership/program-b/add',
            slug: '/membership/program-b/add'
          },
          {
            title: 'Delete Membership',
            link: '/membership/program-b/delete',
            slug: '/membership/program-b/delete'
          }
        ]
      }
    ],
  },
];

我想过滤菜单,即仅显示授予用户的内容。用户只能根据允许的弹头查看菜单,如下所示:

const allowed_slug = [
  '/dashboard/user-dashboard',
  '/inventory/add-inventory',
  '/membership/program-b/view',
  '/membership/program-b/add'
];

使用.filter可以过滤数组的第一层。这是我到目前为止所取得的成就:

function filterMenu(menus, allowed_slug) {
  const result = menus.filter(function (menu_item) {
    return allowed_slug.filter(function(slug) {
      return menu_item.slug.indexOf(slug) > -1;
    }).length;
  });

  return result;
}

理想的输出应如下所示:

o
|-- User Dashboard
|-- Inventory
|   `-- Add Inventory
`-- Membership
    `-- Program B
        |-- View Membership
        `-- Add Membership

问题是,我无法过滤嵌套数组,即children和children的孩子。很感谢任何形式的帮助。 :)

3 个答案:

答案 0 :(得分:5)

您可以检查一个allowdSlug是否以一小段实际对象开头。

var menus = [{ title: 'Supervisor Dashboard', link: '/dashboard/supervisor-dashboard', slug: '/dashboard/supervisor-dashboard' }, { title: 'User Dashboard', link: '/dashboard/user-dashboard', slug: '/dashboard/user-dashboard' }, { title: 'Inventory', slug: '/inventory', children: [{ title: 'Add Inventory', link: '/inventory/add-inventory', slug: '/inventory/add-inventory' }, { title: 'Remove Inventory', link: '/inventory/remove-inventory', slug: '/inventory/remove-inventory' }] }, { title: 'Membership', slug: '/membership', children: [{ title: 'Program A', slug: '/membership/program-a', children: [{ title: 'View Membership', link: '/membership/program-a/view', slug: '/membership/program-a/view' }, { title: 'Add Membership', link: '/membership/program-a/add', slug: '/membership/program-a/add' }, { title: 'Delete Membership', link: '/membership/program-a/delete', slug: '/membership/program-a/delete' }] }, { title: 'Program B', slug: '/membership/program-b', children: [{ title: 'View Membership', link: '/membership/program-b/view', slug: '/membership/program-b/view' }, { title: 'Add Membership', link: '/membership/program-b/add', slug: '/membership/program-b/add' }, { title: 'Delete Membership', link: '/membership/program-b/delete', slug: '/membership/program-b/delete' }] }] }], allowed_slug = ['/dashboard/user-dashboard', '/inventory/add-inventory', '/membership/program-b/view', '/membership/program-b/add'],
    filter = menus => menus
        .filter(({ slug }) => allowed_slug.some(s => s.startsWith(slug)))
        .map(({ title, slug, children = [] }) => {
            children = filter(children);
            return Object.assign({ title, slug }, children.length && { children })
        }),
    result = filter(menus);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:2)

递归是您的朋友在这里:

 // smae signature as your function
 const filterMenu = (menus, allowed) => menus
    // first of all, copy & filter recursively
   .map(({ title, slug, link, children }) => ({ title, slug, link, children: children && filterMenu(children, allowed) }))
   // then remove all that don't have allowed children and are not allowed themself
   .filter(it => it.children && it.children.length || allowed.includes(it.slug));

答案 2 :(得分:2)

尝试使用此功能,看它是否符合您的需求。它基本上使用递归reduce函数创建一个新数组,检查每个元素是否符合slugs数组的条件:

const menu = [ { title: 'Supervisor Dashboard', link: '/dashboard/supervisor-dashboard', slug: '/dashboard/supervisor-dashboard' }, { title: 'User Dashboard', link: '/dashboard/user-dashboard', slug: '/dashboard/user-dashboard' }, { title: 'Inventory', slug: '/inventory', children: [ { title: 'Add Inventory', link: '/inventory/add-inventory', slug: '/inventory/add-inventory' }, { title: 'Remove Inventory', link: '/inventory/remove-inventory', slug: '/inventory/remove-inventory' }, ] }, { title: 'Membership', slug: '/membership', children: [ { title: 'Program A', slug: '/membership/program-a', children: [ { title: 'View Membership', link: '/membership/program-a/view', slug: '/membership/program-a/view' }, { title: 'Add Membership', link: '/membership/program-a/add', slug: '/membership/program-a/add' }, { title: 'Delete Membership', link: '/membership/program-a/delete', slug: '/membership/program-a/delete' } ] }, { title: 'Program B', slug: '/membership/program-b', children: [ { title: 'View Membership', link: '/membership/program-b/view', slug: '/membership/program-b/view' }, { title: 'Add Membership', link: '/membership/program-b/add', slug: '/membership/program-b/add' }, { title: 'Delete Membership', link: '/membership/program-b/delete', slug: '/membership/program-b/delete' } ] } ], }, ]; const allowed_slug = [ '/dashboard/user-dashboard', '/inventory/add-inventory', '/membership/program-b/view', '/membership/program-b/add' ];

const filterMenu = (menu, allowed) =>
    menu.reduce((a, {title, link, slug, children = []}) =>
        (children = filterMenu(children, allowed), (children.length && (a = [...a, {title, slug, children}])) || (allowed.includes(slug) && (a = [...a, {title, link, slug}])), a), []);

console.log(filterMenu(menu, allowed_slug));

该功能的可读性更高的版本:

const filterMenu = (menu, allowed) =>
    menu.reduce((array, {title, link, slug, children = []}) => {
        children = filterMenu(children, allowed);
        if (children.length) {
            array.push({title, slug, children});
        } else if (allowed.includes(slug)) {
            array.push({title, link, slug});
        }
        return array;
    }, []);