简体   繁体   English

浏览对象数组并删除对象

[英]browse object array and delete objects

I have that array of objects that I want to be able to browse and leave in the object only those that comply with the array of permissions that are specified in the array of objects as data, including removing also the objects that are inside children我有我希望能够浏览的对象数组,并且只将那些符合对象数组中指定的权限数组作为数据的对象保留在对象中,包括还删除子对象中的对象

permissions= ["create:users", "read:profile", "read:roles", "read:users", "update:users", "write:messages"]  



[
          {
            title: 'Dashboard',
            icon: 'home-outline',
            link: '/',
            home: true,
            children: undefined,
          },
          {
            title: 'Profile',
            icon: 'lock-outline',
            link: '/profile',
            children: undefined,
          },
          {
            title: 'Users',
            icon: 'person-outline',
            link: '/users',
            data: ['read:user','create:user'],
            children: [
              {
                title: 'Users',
                link: '/users',
                data: 'read:user',
              },
              {
                title: 'Create User',
                link: '/edit-user',
                data: 'create:user',
              },

           ],

First, the values in expectedPermission array need to match those in the permissions array.首先, expectedPermission数组中的值需要与permissions数组中的值相匹配。 If you don't control the values, you'll need to map them somehow.如果您不控制这些值,则需要以某种方式映射它们。 In your example, "create:users" and "create:user" won't match.在您的示例中,“create:users”和“create:user”不匹配。

Assuming you resolve that, something like this should get you on the right track:假设你解决了这个问题,这样的事情应该会让你走上正轨:

function hasExpectedPermissions(expectedPermissions) {
  return expectedPermissions.filter(function (expectedPermission) {
    // Only return expected permissions that are NOT found in the permissions array
    return permissions.indexOf(expectedPermission) === -1;
  }).length === 0; // If didn't find any MISSING permissions, we're good
}

var filteredPages = pages.filter(function (page) {
  return !page.data
    || hasExpectedPermissions(page.data);
});

If it's possible for child elements to be filtered, you'd need to write a recursive function.如果可以过滤子元素,则需要编写递归函数。 Something like:就像是:

function getAllowedPages(pages) {
  return pages.filter(function (page) {
    return !page.data
      || hasExpectedPermissions(page.data);
  }).map(page) {
    if (page.children) {
      page.children = getAllowedPages(page.children);
    }
    return page;
  });
}

My understanding is that you'd like to remove from arrayofObjects.data any permissions not listed in the permissions list on top and delete the respective children too.我的理解是,您想从 arrayofObjects.data 中删除顶部权限列表中未列出的任何权限,并删除相应的子项。
If this is the case, a solution could be the following.如果是这种情况,解决方案可能如下。
It's a bit complicated but functional.它有点复杂但功能齐全。
I hope that's useful for you.我希望这对你有用。

 // ------ // Dataset // ------ var permissions= ["create:users", "read:profile", "read:roles", "read:users", "update:users", "write:messages"]; var arrayofObjects = [ { title: 'Dashboard', icon: 'home-outline', link: '/', home: true, children: undefined, }, { title: 'Profile', icon: 'lock-outline', link: '/profile', children: undefined, }, { title: 'Users', icon: 'person-outline', link: '/users', data: ['read:user','create:user'], children: [ { title: 'Users', link: '/users', data: 'read:user', }, { title: 'Create User', link: '/edit-user', data: 'create:user', } ] }, { title: 'Users', icon: 'person-outline', link: '/users', data: ['create:role','create:user','create:test'], children: [ { title: 'Create Role', link: '/edit-role', data: 'create:role', }, { title: 'Create User', link: '/edit-user', data: 'create:user', }, { title: 'Create Test', link: '/edit-test', data: 'create:test', } ] } ] // ------ // Make permissions and object.data match // ------ var elaboratedPermissions = []; permissions.forEach(myTransformation); function myTransformation(permission){ let aux = []; aux = permission.split(":"); if (aux[1].endsWith("s")){ aux[1] = aux[1].slice(0,-1); } elaboratedPermissions.push(aux[0].concat(":", aux[1])); } // ------ // Filter objects // ------ var arrayofObjects_Filtered = []; // filtered array of objects arrayofObjects.forEach(filterObjects); var unsupportedPermissionsIndex; // auxiliary array of unsupported permissions index var unsupportedChildrenIndex; // auxiliary array of unsupported permissions index //separate variables/functions used for permission and children in case there is any mismatch or order difference) // If object has no property data just return it else clean unsupported permissions function filterObjects(object){ var objectDataAux = object.data; // auxiliary var to hold object.data //var objectChildrenAux = object.children; // auxiliary var to hold object.children unsupportedPermissionsIndex = []; unsupportedChildrenIndex = []; if (object.hasOwnProperty('data')){ object.data.forEach((givenPermission, index, givenPermissions) => {getUnsupportedPermissions(givenPermission, index, givenPermissions)}); if (unsupportedPermissionsIndex.length > 0){ // remove unsupported permissions from object.data object.data = object.data.filter((givenPermission, index, givenPermissions) => { if (!(unsupportedPermissionsIndex.includes(index))){ return givenPermission; } }); object.children.forEach((child, index, children) => {getUnsupportedChildren(child.data, index, children)}); object.children = object.children.filter((child, index, children) => { if (!(unsupportedChildrenIndex.includes(index))){ return child; } }); } } arrayofObjects_Filtered.push(object); console.log("object: ", object); } // Get unsupported permissions (index) function getUnsupportedPermissions(givenPermission, index, givenPermissions){ if (!(elaboratedPermissions.includes(givenPermission))){ unsupportedPermissionsIndex.push(index); } } // Get unsupported children (index) function getUnsupportedChildren(child, index, children){ if (!(elaboratedPermissions.includes(child))){ unsupportedChildrenIndex.push(index); } } // ------ // See result in console // ------ //console.log(arrayofObjects_Filtered);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM