Lodash filter by array of array properties

I have an array of users who have permissions to access the array of properties, and I want to filter out users who have specific rights. I would like to filter by array, so if I wanted all users to have full rights ['full'] or users with full and right ['full', 'edit']. I am new to lodash and I think I can relate to each other, but I'm not sure if these are effective ways to do this.

Here is my pluker: http://plnkr.co/edit/5PCvaDJaXF4uxRowVBlK?p=preview

Result ['full']:

[{
    "name": "Company1 Admin",
    "rights": [
      "full"
    ]
  },
  {
    "name": "FullRights Company1",
    "rights": [
      "full","review"
    ]
  }]

Result ['full', 'edit']:

[{
    "name": "Company1 Admin",
    "rights": [
      "full"
    ]
  },
  {
    "name": "FullRights Company1",
    "rights": [
      "full","review"
    ]
  },
  {
    "name": "EditRights Company1",
    "rights": [
      "edit"
    ]
  }]

code:

var users = [
      {
        "name": "Company1 Admin",
        "rights": [
          "full"
        ]
      },
      {
        "name": "FullRights Company1",
        "rights": [
          "full","review"
        ]
      },
      {
        "name": "ApproveRights Company1",
        "rights": [
          "approve","review"
        ]
      },
      {
        "name": "EditRights Company1",
        "rights": [
          "edit"
        ]
      },
      {
        "name": "ReviewRights Company1",
        "rights": [
          "review"
        ]
      },
      {
        "name": "NoRights Company1",
        "rights": [
          "none"
        ]
      }
    ];
        
var tUsers = [];
var filterRights = ['full','edit'];
_.forEach(users, function(user) {
    if (_.intersection(user.rights, filterRights).length > 0) {
      tUsers.push(user);
    }
}) ;        
        
//console.log('users', JSON.stringify(users, null, 2)); 
console.log('tUsers', JSON.stringify(tUsers, null, 2));  
        
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
Run codeHide result
+4
3

docs

_. (, , thisArg);

  • collection (Array | Object | string): .

  • [predicate = _. identity] (Function | Object | string): , .

  • [thisArg] (*): .


, .

  • 10

.

, -, .

var users = [/* Your user data here */];
function filterByRights (users, rights) {
    return _.filter(users, function (user) {
        return _.any(user.rights, function (right) {
            return _.contains(rights, right);
        });
    });
}
filterByRights(users, ['full', 'edit']); // [/*Users with full or edit rights*/]

, , . lodash, any contains


. .

  • . , , , lodash, . , , .

  • _.any , _.intersection. _.intersection , , . _.any , , , . , ""

  • , , , " lodash". lodash .

+3

@t3dodson. , (4.17.4) Lodash:

function filterByRights (users, rights) {
  return _.filter(users, function (user) {
    return _.some(user.rights, function (right) {
      return _.includes(rights, right);
    });
  });
}

Changelog:

_.contains _.includes

_.any _.some

+2

, intersection() ( ). flow():

_.filter(users, _.flow(
    _.property('rights'), 
    _.partial(_.intersection, filterRights), 
    _.size
));

() rights intersection(). filterRights. , size() thruthy/falesy ().

0

Source: https://habr.com/ru/post/1621553/


All Articles