【问题标题】:Filter Not Filtering Records过滤不过滤记录
【发布时间】:2018-04-26 10:41:36
【问题描述】:

我正在尝试在$scope.employees 中查找在$scope.allEmployeeGroups 中没有匹配记录的记录,但过滤器不是过滤记录,即使我确定应该只有一些不匹配的记录,它返回所有记录。对于每条记录, indexOf == -1 当我知道它不应该时。我无法弄清楚我做错了什么。这是我的代码:

    function getNonGroupEmployees() {
    var arr = $scope.employees.filter(function (item) {
        return $scope.allEmployeeGroups.indexOf(item.EmployeeId) === -1;
    })
    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; }

非常感谢任何帮助。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    不要使用 .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; }
    

    【讨论】:

    • 感谢您的帮助。不幸的是,上面的代码抛出了错误。
    • $scope.allEmployeeGroups 列表是否包含与 $scope.employees 相同的类型对象?
    • 没有。对象不同。他们唯一共享的是 EmployeeId 字段。
    • 能否分享一下这两个对象的结构?
    • 我已经更正了我的代码块,但不确定它是否能解决您的问题。
    【解决方案2】:

    感谢找到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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 2011-08-10
      • 1970-01-01
      • 2020-02-18
      • 2017-08-18
      • 2015-02-13
      • 2017-04-29
      • 1970-01-01
      相关资源
      最近更新 更多