Javascript-使用映射或过滤器基于值的存在返回数组成员的子集

Javascript-使用映射或过滤器基于值的存在返回数组成员的子集,javascript,Javascript,我已经对此进行了研究,虽然有很多关于这些概念的信息,但我很难理解如何将所有这些想法结合起来以解决我的问题 我有一个数组: [ { route: ["", "scheduler"], name: "scheduler", settings: { icon: "user", auth: true, roles: ["Employee", "Admin"], pos: "left" }, moduleId: P

我已经对此进行了研究,虽然有很多关于这些概念的信息,但我很难理解如何将所有这些想法结合起来以解决我的问题

我有一个数组:

[
  {
    route: ["", "scheduler"],
    name: "scheduler",
    settings: {
      icon: "user",
      auth: true,
      roles: ["Employee", "Admin"],
      pos: "left"
    },
    moduleId: PLATFORM.moduleName("../components/scheduler/scheduler"),
    nav: true,
    title: "scheduler"
  },
  {
    route: "jobs",
    name: "jobs",
    moduleId: PLATFORM.moduleName("../components/jobs/jobsList"),
    settings: {
      icon: "list",
      auth: true,
      roles: ["Employee"],
      pos: "left"
    },
    nav: true,
    title: "Jobs"
  },
  {
    route: "clients",
    name: "clients",
    moduleId: PLATFORM.moduleName("../components/clients/clientList/clientList"),
    title: "Clients",
    nav: true,
    settings: {
      nav: [
        {
          icon: "list",
          route: "clients/ClientsList",
          name: "clientList",
          moduleId: PLATFORM.moduleName(
            "../components/clients/clientList/clientList"
          ),
          href: "#clients/clientsList",
          title: "Client List",
          roles: ["Employee", "Admin"]
        },
        {
          icon: "user",
          route: "clients/create",
          name: "aboutTeam",
          moduleId: PLATFORM.moduleName(
            "../components/clients/clientCreate/clientCreate"
          ),
          href: "#clients/Create",
          title: "Create Client",
          roles: ["Employee", "Admin"]
        }
      ],
      icon: "user",
      auth: true,
      roles: ["Employee", "Admin"],
      pos: "left"
    }
  },
  {
    route: "clients/ClientsList",
    name: "clientList",
    moduleId: PLATFORM.moduleName("../components/clients/clientList/clientList"),
    settings: {
      icon: "list",
      auth: true,
      roles: ["Employee", "Admin"],
      pos: "left"
    }
  },
  {
    route: "clients/create",
    name: "aboutTeam",
    moduleId: PLATFORM.moduleName("../components/clients/clientCreate/clientCreate"),
    settings: {
      icon: "user",
      auth: true,
      roles: ["Employee", "Admin"],
      pos: "left"
    }
  },
  {
    route: "companyDetail",
    name: "companyDetail",
    moduleId: PLATFORM.moduleName("../components/administration/companyDetail/companyDetail"),
    settings: {
      icon: "list",
      auth: true,
      roles: ["Employee", "Admin"],
      pos: "left"
    },
    nav: true,
    title: "Company Detail"
  },
  {
    route: "user",
    name: "user",
    moduleId: PLATFORM.moduleName("../components/administration/user/user"),
    settings: {
      icon: "user",
      auth: true,
      roles: ["Employee", "Admin"],
      pos: "right"
    },
    nav: true,
    title: "User"
  },
  {
    route: "notFound",
    name: "notFound",
    settings: { auth: false },
    moduleId: PLATFORM.moduleName("../components/notFound/notFound"),
    nav: false,
    title: "Not Found"
  }
]
每个都有一个设置选项,其中有一个角色数组

我想循环检查每个数组成员,比如说,该成员是否担任“Admin”角色,然后使用“push”将其添加到新数组中

然而,我希望学习JavaScript并扩展我的知识。我想我应该使用“map”选项和“fat arrow”函数来简化代码,这样我就有了一个新的数组,其中包含了所有项目,其中角色包含一个userRole of admin

这是我的尝试:

console.log(this.menu.map(r => r.filter(f => f.settings.roles === userRole)));
完整的功能是

public userMenu(userName: string, userRole: string): any {
const fullMenu = this.menuList();
console.log("userRole: ", userRole);
console.log(this.menu.map(r => r.filter(f => f.settings.roles === userRole)));
}
“userRole”存在并显示在控制台中,但我收到以下错误:

未捕获的TypeError:无法读取未定义的属性“map”

如何构造map函数以使其工作,从而只返回上面较大数组中角色数组中包含“Admin”的项目

我想循环检查每个数组成员,比如说,该成员是否担任“Admin”角色,然后使用“push”将其添加到新数组中

函数返回新数组,因此您无需自己创建新数组。如果需要在数组中循环,请改用
forEach

未捕获的TypeError:无法读取未定义的属性“map”

请显示设置此菜单的代码。


log(this.menu.map(r=>r.filter(f=>f.settings.roles===userRole));

  • map
    filter
    reduce
    只能与数组一起使用。上面的代码显示,您对对象
    r
    使用
    filter
    (正如
    菜单
    是一个对象数组)

  • f.settings.roles===userRole
    :userRole是字符串,而settings.roles是数组,因此您必须使用
    包含
    (或
    索引of
    )作为筛选条件

因为您只想筛选满足条件的项,所以使用
filter
就足够了,如下所示


menu.filter(r=>r.settings.roles&&r.settings.roles.includes('Admin'))

似乎未定义
此.menu
-是什么设置了
此.menu