0

I'm trying to find records in $scope.employeesthat do not have a matching record in $scope.allEmployeeGroups, but the filter is not Filtering records, even though I know for sure that there should only be a few unmatched, it returns all of the records. For each record, the indexOf == -1 when I know that it should not. I can't figure out what I am doing wrong. Here is my code:

    function getNonGroupEmployees() {
    var arr = $scope.employees.filter(function (item) {
        return $scope.allEmployeeGroups.indexOf(item.EmployeeId) === -1;
    })
    return arr;
}

Employee Object:

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

Group Object:

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

Any assistance is greatly appreciated.

Rani Radcliff
  • 4,152
  • 4
  • 28
  • 49

2 Answers2

0

Instead of searching object with .IndexOf(), use property matching. Object will not match if two objects do not have same reference.

Try with following code block:

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

AS requested here are data structures:

Employee:

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

Employee Group

            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; }
Syedur
  • 364
  • 3
  • 9
0

Thanks to answer found Here, here is what eventually worked:

    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;
}
Rani Radcliff
  • 4,152
  • 4
  • 28
  • 49