过滤不过滤记录

时间:2017-11-13 16:55:09

标签: javascript angularjs

我正在尝试查找$scope.allEmployeeGroups function getNonGroupEmployees() { var arr = $scope.employees.filter(function (item) { return $scope.allEmployeeGroups.indexOf(item.EmployeeId) === -1; }) return arr; } 中没有匹配记录的记录,但过滤器不是过滤记录,即使我确定应该只有几个无与伦比的,它返回所有的记录。对于每条记录,当我知道它不应该是indexOf == -1。我无法弄清楚我做错了什么。这是我的代码:

public System.Guid EmployeeId { get; set; }
    public Nullable<System.Guid> SiteId { get; set; }
    public string SiteName { get; set; }
    public string DisplayName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string Suffix { get; set; }
    public string Alias { get; set; }
    public Nullable<System.DateTime> DOB { get; set; }
    public string SsnLastFour { get; set; }
    public string Email { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public bool IsActive { get; set; }
    public bool IsLoginEnabled { get; set; }
    public Nullable<System.DateTime> LastLogin { get; set; }
    public Nullable<System.Guid> SignatureTypeId { get; set; }
    public string SignatureType { get; set; }
    public string NumberHome { get; set; }
    public string NumberCell { get; set; }
    public bool IsSuperUser { get; set; }
    public bool IsDeleted { get; set; }
    public System.DateTime Created { get; set; }
    public System.DateTime LastModified { get; set; }
    public Nullable<System.Guid> ModifiedByEmployeeId { get; set; }
    public string ApiKey { get; set; }

员工对象:

            public Guid EmployeeGroupId { get; set; }
        public Guid SiteId { get; set; }
        public Guid EmployeeId { get; set; }
        public Guid SiteGroupId { get; set; }
        public bool IsDeleted { get; set; }
        public DateTime Created { get; set; }
        public DateTime LastModified { get; set; }
        public Guid? ModifiedByEmployeeId { get; set; }
        public string SiteName { get; set; }
        public string EmployeeName { get; set; }
        public string SiteGroupName { get; set; }
        public string ModifiedByEmployeeName { get; set; }

组对象:

guard

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

不使用.IndexOf()搜索对象,而是使用属性匹配。如果两个对象没有相同的引用,则对象将不匹配。

尝试使用以下代码块:

function getNonGroupEmployees() {
        var arr = $scope.employees.filter(function (item) {
            return $scope.allEmployeeGroups.find(function(p){ 
                return p.EmployeeId == item.EmployeeId
            })=== null;
        })
        return arr;
}

这里要求的是数据结构:

员工:

public System.Guid EmployeeId { get; set; }
        public Nullable<System.Guid> SiteId { get; set; }
        public string SiteName { get; set; }
        public string DisplayName { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string Suffix { get; set; }
        public string Alias { get; set; }
        public Nullable<System.DateTime> DOB { get; set; }
        public string SsnLastFour { get; set; }
        public string Email { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool IsActive { get; set; }
        public bool IsLoginEnabled { get; set; }
        public Nullable<System.DateTime> LastLogin { get; set; }
        public Nullable<System.Guid> SignatureTypeId { get; set; }
        public string SignatureType { get; set; }
        public string NumberHome { get; set; }
        public string NumberCell { get; set; }
        public bool IsSuperUser { get; set; }
        public bool IsDeleted { get; set; }
        public System.DateTime Created { get; set; }
        public System.DateTime LastModified { get; set; }
        public Nullable<System.Guid> ModifiedByEmployeeId { get; set; }
        public string ApiKey { get; set; }

员工组

            public Guid EmployeeGroupId { get; set; }
        public Guid SiteId { get; set; }
        public Guid EmployeeId { get; set; }
        public Guid SiteGroupId { get; set; }
        public bool IsDeleted { get; set; }
        public DateTime Created { get; set; }
        public DateTime LastModified { get; set; }
        public Guid? ModifiedByEmployeeId { get; set; }
        public string SiteName { get; set; }
        public string EmployeeName { get; set; }
        public string SiteGroupName { get; set; }
        public string ModifiedByEmployeeName { get; set; }

答案 1 :(得分:0)

感谢找到答案Here,以下是最终有效的内容:

    function getNonGroupEmployees() {
    var result = $scope.employees.filter(function (o1) {
        return !$scope.allEmployeeGroups.some(function (o2) {
            return o1.EmployeeId === o2.EmployeeId;          // assumes unique id
        });
    })
    return result;
}