首页 > 解决方案 > 使用 3 阵列检查能力

问题描述

我必须创建一个功能来检查能力和显示模块,这取决于能力。在每个模板中,我必须再次检查 subMobules 功能

我必须用结果对另一个数组进行排序

userAbilities 是这样的:

{ id: 1, name: 'moduleAuth1', title: 'moduleAuth1'},
{ id: 2, name: 'moduleAuth2', title: 'moduleAuth2'},

和我的模块:

[ {
  name: 'module1',
  auth: [moduleAuth1, moduleAuth2 ],
  subModule: [
    {
      subName: 'foo',
      subAuth: [subAuth1, subAuth2],
    },
   {
      subName: 'foo2',
      subAuth: [subAuth1, subAuth2, subAuth3],
    },
{
  name: 'module1',
  auth: [moduleAuth1, moduleAuth2, moduleAuth3 ],
  subModule: [
    {
      subName: 'foo',
      subAuth: [moduleAuth1, moduleAuth2],
    },
   {
      subName: 'foo2',
      subAuth: [moduleAuth1],
    },
}
  ]

这个函数需要等待用户的能力并返回一个数组,

我尝试使用 multi forEach 来做到这一点,但它真的很难阅读。

我唯一有效的功能,只测试一个身份验证,他不是异步的:

moduleFilter (module: any) {
    const myStore = this.userAuth
    const moduleFilter: any[] = []
    module.forEach(moduleEl => {
      if (moduleEl.auth[0] === true) {
        moduleFilter.push(moduleEl)
      }
      myStore.forEach(userAuth => {
        console.log(moduleEl.auth)
        if (moduleEl.auth[0] === userAuth.name) {
          moduleFilter.push(moduleEl)
        }
      })
    })
    console.log(moduleFilter)
    return moduleFilter
  }

标签: arraystypescriptsortingmultidimensional-array

解决方案


推荐阅读