使用连接字符串过滤数组列表 angular js

Filter Array List using concatenated string angular js

我正在尝试使用过滤器过滤字符串数组

var employeeRole=['Manager', 'Employee', 'Director', 'Supervisor', 'TeamLead'];
var permittedRoles='Manager|Director';

我如何找到 permittedRoles 中的任何一个是否包含在 employeeRole

您可以通过 splitting the permittedRoles by | and checking if any of the values (use of some) in the resultant array is contained (use of indexOf) 在 employeeRole.

中完成

请参阅下面的完整示例:

angular
  .module('app', [])
  .controller('ctrl', function($scope) {
    $scope.employeeRole = ['Manager', 'Employee', 'Director', 'Supervisor', 'TeamLead'];
    $scope.permittedRoles = 'Manager|Director';

    $scope.check = function() {
      console.log(doCheck())
    };

    /**
     * Return `true` if any of the values in `permittedRoles` separated by `|` is contained in the array `employeeRole`, `false` otherwise.
     */
    function doCheck() {
      return $scope.permittedRoles.split('|')
        .some(
          function(role) {
            return $scope.employeeRole.indexOf(role) !== -1;
          }
        );
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">

  <label for="permittedRoles">permittedRoles: </label>
  <input id="permittedRoles" ng-model="permittedRoles" />

  <hr/>
  <a href="#" ng-click="check()">Check (click me!)</a>
  <hr/>
  <label>employeeRole:</label>
  <br/>
  <div ng-repeat="role in employeeRole">
    {{role}}
  </div>
</div>