如何根据条件过滤数组

时间:2021-01-09 14:14:49

I am working on a Role Based permissions, I have following array,

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

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' }
  ]
}

and I am trying to get the following output

我试图获得以下输出

  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,
}]

the condition to generate the result would be , for a given model and property , if permission is allow , it should be true, else false.

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

I have tried by writing if condition and assign the value true , if condition matches . But for the second array the values are overriding

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

From the Roles array , Iam passing each array to the following function and returning the result

从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 个解决方案

#1


1  

You can use filter to find the array of permissions from a certain user:

您可以使用过滤器来查找特定用户的权限数组:

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

Use reduce to create your object and switch for each property.

使用reduce创建对象并切换每个属性。

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
);

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));

#2


1  

As you mentioned in question

正如你提到的那样

Role Based permissions, I have following array

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

You can define your permission object like this

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

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

You can iterate your array using reduce() function and use that permissionObj inside of reduce() function.

您可以使用reduce()函数迭代数组,并在reduce()函数中使用该permissionObj。

DEMO

DEMO

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)
.as-console-wrapper {  max-height: 100% !important;  top: 0;}

#1


1  

You can use filter to find the array of permissions from a certain user:

您可以使用过滤器来查找特定用户的权限数组:

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

Use reduce to create your object and switch for each property.

使用reduce创建对象并切换每个属性。

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
);

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));

#2


1  

As you mentioned in question

正如你提到的那样

Role Based permissions, I have following array

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

You can define your permission object like this

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

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

You can iterate your array using reduce() function and use that permissionObj inside of reduce() function.

您可以使用reduce()函数迭代数组,并在reduce()函数中使用该permissionObj。

DEMO

DEMO

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)
.as-console-wrapper {  max-height: 100% !important;  top: 0;}