🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] >[success] # 数组的filter()过滤方法 js中数组的filter()可以按照条件过滤返回一个新的数组,例如你又一个需求,需要按照角色权限显示菜单,菜单是个数组的格式如下: <br/> ~~~ var menuList= [ { "code": "SYS100", "title": "数据管理", "icon": "bimicon-zuzhiguanli", "children": [{ "code": "SYS110", "title": "基础数据", "icon": "bimicon-zuzhi" }] }, { "code": "SYS200", "title": "业务管理", "icon": "bimicon-biaodan" }, { "code": "SYS400", "title": "APP管理", "icon": "bimicon-shebei", "children": [{ "code": "SYS410", "title": "版本管理", "icon": "bimicon-gongchengjiegou", "componentPath": "@/pages/app-management/VersionManagement" }] }] ~~~ 现在如果是管理员角色就显示APP管理这个菜单其他角色不显示这个菜单,代码如下: ~~~ menuList ( ) { let isAdmin = true // 非管理员 if(!isAdmin){ menuList = menuList.filter(item => { return item.code !== 'SYS410' }) } } 返回结果为: [{ "code": "SYS100", "title": "数据管理", "icon": "bimicon-zuzhiguanli", "children": [{ "code": "SYS110", "title": "基础数据", "icon": "bimicon-zuzhi" }] }, { "code": "SYS200", "title": "业务管理", "icon": "bimicon-biaodan" }] ~~~