javascript - 以 Angular 过滤数组数据

标签 javascript angularjs

$scope.currentEvent = Object { item_id: "10535", name: "johnny", user_type: "1",category_id: "5"}, Object { item_id: "10534", name: "smith", user_type: "1",category_id: "6"}, Object { item_id: "10536", name: "Greg", user_type: "1",category_id: "7"};


$scope.users = Object { item_id: "11355", name: "mathew", user_type: "1",category_id: "7"}, Object { item_id: "10336", name: "Greg", user_type: "2",category_id: "7"}, Object { item_id: "10545", name: "powell", user_type: "1",category_id: "7"}, Object { item_id: "10456", name: "micheal", user_type: "3",category_id: "7"};

需要根据 user_type 进行过滤

Expected result = $scope.validUser = Object { item_id: "11355", name: "mathew", user_type: "1",category_id: "7"},  Object { item_id: "10545", name: "powell", user_type: "1",category_id: "7"};

我的工作 我使用下面的代码对我不起作用

$scope.validUser = $scope.validUser.filter(function($scope.currentEvent){
    return $scope.validUser.user_type === $scope.currentEvent.user_type;
});

请指导:我们该如何解决这个问题?

还解释一下我们获得结果的方式 - 如何在 angularjs 中过滤数据?

最佳答案

$scope.validUser = $scope.users.filter(function(obj){
    return obj.user_type === $scope.currentEvent.user_type;
});
  • 过滤 users 数组而不是 validUser
  • 过滤器参数是当前迭代的对象,因此不要在其中使用 $scope

关于javascript - 以 Angular 过滤数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46364770/

相关文章:

javascript - AngularJS。调用 angular-ui 模态时清除 $timeout

javascript - ngOptions 更改时 ngModel 未更新

angularjs - Protractor E2E 使用 ng-file-upload 测试多个文件上传

javascript - HTML 标签 <a> href 和 onclick

javascript - 使用 Ajax 的 OAuth2 隐式授权

angularjs - 如何捕捉 Angular 1.x 网站上的出站链接点击?

javascript - C#.net 相当于 JS .on()

javascript - 谷歌 JavaScript 表

javascript - 多个 OfflineAudioContext 崩溃浏览器

javascript - 从 p 标签中删除文本并使用 jquery 添加一个类