gpt4 book ai didi

javascript - 以 Angular 过滤数组数据

转载 作者:行者123 更新时间:2023-11-28 04:10:12 24 4
gpt4 key购买 nike

$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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com