如何根据条件过滤数组

时间:2018-04-24 11:01:11

标签: javascript arrays typescript angular5

我正在开发基于角色的权限,我有以下数组,

let Roles = {
  [
    { model: 'user', property: 'find', permission: 'allow' },
    { model: 'user', property: 'create', permission: 'allow' },
    { model: 'user', property: 'update', permission: 'deny' },
    { model: 'user', property: 'delete', permission: 'deny' }
  ],
  [
    { model: 'registration', property: 'find', permission: 'allow' },
    { model: 'registration', property: 'create', permission: 'deny' },
    { model: 'registration', property: 'update', permission: 'deny' },
    { model: 'registration', property: 'delete', permission: 'deny' }
  ]
  [
    { model: 'schedule', property: 'find', permission: 'allow' },
    { model: 'schedule', property: 'create', permission: 'allow' },
    { model: 'schedule', property: 'update', permission: 'allow' }
    { model: 'schedule', property: 'delete', permission: 'deny' }
  ]
}

我试图获得以下输出

  let userPermissions = [{
  'menu_name': 'user',
  'canFetchData': true,
  'canCreateData': true,
  'canUpdateData': false,
  'canDeleteData': false,
}]

let registrationPermissions = [{
  'menu_name': 'registration',
  'canFetchData': true,
  'canCreateData': false,
  'canUpdateData': false,
  'canDeleteData': false,
}]

let schedulePermissions = [{
  'menu_name': 'schedule',
  'canFetchData': true,
  'canCreateData': true,
  'canUpdateData': true,
  'canDeleteData': false,
}]

生成结果的条件是,对于给定的模型和属性,如果允许权限,则应为true,否则为false。

我已尝试过写条件并将值赋值为true,如果条件匹配。但对于第二个数组,值是重写

从Roles数组中,Iam将每个数组传递给以下函数并返回结果

  private canFetchData;
  private canCreateData;
  private canUpdateData;
  private canDeleteData;

filterAndApplyPermission(data) {
for (let i = 0; i < data.length; i++) {
  if (data[i].property === 'find' && data[i].permission === 'ALLOW') {
    this.canFetchData = true;
  } else if (data[i].property === 'create' && data[i].permission === 'ALLOW') {
    this.canCreateData = true;
  } else if (data[i].property === 'update' && data[i].permission === 'ALLOW') {
    this.canUpdateData = true;
  } else if (data[i].property === 'delete' && data[i].permission === 'ALLOW') {
    this.canDeleteData = true;
  }
}

const grouped_permission = {
  'menu': data[0].model,
  'canFetchData': this.canFetchData,
  'canCreateData': this.canCreateData,
  'canUpdateData': this.canUpdateData,
  'canDeleteData': this.canDeleteData,
};
   return grouped_permission;
 }

2 个答案:

答案 0 :(得分:1)

您可以使用过滤器查找某个用户的权限数组:

roleInfo
.filter(roles=>roles[0].model==="schedule")[0]

使用reduce为每个属性创建对象和switch

rolesForUser
.reduce(
  (info,item)=>{
    switch(item.property) {
      case 'find':
        info.canFetchData = (item.permission==='allow')?true:false
        break;
      //other cases
    }
    return info;
  },
  {menu_name : user}//initial info object with menu_name set to user
);

&#13;
&#13;
const roleInfo = [
  [
    { model: 'user', property: 'find', permission: 'allow' },
    { model: 'user', property: 'create', permission: 'allow' },
    { model: 'user', property: 'update', permission: 'deny' },
    { model: 'user', property: 'delete', permission: 'deny' }
  ],
  [
    { model: 'registration', property: 'find', permission: 'allow' },
    { model: 'registration', property: 'create', permission: 'deny' },
    { model: 'registration', property: 'update', permission: 'deny' },
    { model: 'registration', property: 'delete', permission: 'deny' }
  ],
  [
    { model: 'schedule', property: 'find', permission: 'allow' },
    { model: 'schedule', property: 'create', permission: 'allow' },
    { model: 'schedule', property: 'update', permission: 'allow' },
    { model: 'schedule', property: 'delete', permission: 'deny' }
  ]
];

const getRoleForUser = (user,roleInfo) =>
  roleInfo
  .filter(roles=>roles[0].model===user)[0]
  .reduce(
    (info,item)=>{
      switch (item.property) {
        case 'find':
          info.canFetchData = (item.permission==='allow')?true:false
          break;
        case 'create':
          info.canCreateData = (item.permission==='allow')?true:false
          break;
        case 'update':
          info.canUpdateData = (item.permission==='allow')?true:false
          break;
        case 'delete':
          info.canDeleteData = (item.permission==='allow')?true:false
          break;
        default:
          throw new Error(`${item.property} is an unknown permission`);
      }
      return info;
    },
    {menu_name : user}
  );

console.log(getRoleForUser("registration",roleInfo));
&#13;
&#13;
&#13;

答案 1 :(得分:1)

正如你提到的那样

  

基于角色的权限,我有以下数组

您可以像这样定义权限对象

const permissionObj = {
    find: 'canFetchData',
    create: 'canCreateData',
    update: 'canUpdateData',
    delete: 'canDeleteData'
}

您可以使用reduce()函数迭代数组,并使用permissionObj函数内的reduce()

<强>样本

&#13;
&#13;
const dataObj =[[{
            model: 'user',
            property: 'find',
            permission: 'allow'
        }, {
            model: 'user',
            property: 'create',
            permission: 'allow'
        }, {
            model: 'user',
            property: 'update',
            permission: 'deny'
        }, {
            model: 'user',
            property: 'delete',
            permission: 'deny'
        }],
         [{
            model: 'registration',
            property: 'find',
            permission: 'allow'
        }, {
            model: 'registration',
            property: 'create',
            permission: 'deny'
        }, {
            model: 'registration',
            property: 'update',
            permission: 'deny'
        }, {
            model: 'registration',
            property: 'delete',
            permission: 'deny'
        }],
         [{
            model: 'schedule',
            property: 'find',
            permission: 'allow'
        }, {
            model: 'schedule',
            property: 'create',
            permission: 'allow'
        }, {
            model: 'schedule',
            property: 'update',
            permission: 'allow'
        }, {
            model: 'schedule',
            property: 'delete',
            permission: 'deny'
        }]
    ],
    permissionObj = {
        find: 'canFetchData',
        create: 'canCreateData',
        update: 'canUpdateData',
        delete: 'canDeleteData'
    };

let result = dataObj.reduce((r, arr) => {
    let model = '',
        obj = arr.reduce((p, o) => {
            model = o.model;
            p[permissionObj[o.property]] = o.permission.toUpperCase() === 'ALLOW';
            return p;
        }, {})
    r[`${model}Permissions`] = [Object.assign(obj, {
        menu_name: model
    })];
    return r;
}, {});

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