如何基于多个属性筛选记录并获取计数

How to filter records based on more than 1 property and get count

本文关键字:获取 记录 筛选 何基于 属性      更新时间:2023-09-26

我的控制器中有一个数据,如下所示:

$scope.items = [
{
  id: "0",
  name: "Nurse1",
  icon: "rooms_nurse.svg",
  type: "nurse",
  location: build-A
},
{
  id: "1",
  name: "Nurse2",
  icon: "rooms_nurse.svg",
  type: "nurse",
  location: build-A
},
{
  id: "1",
  name: "Dr",
  icon: "rooms_dr.svg",
  type: "Dr",
  location: build-B
},
{
  id: "2",
  name: "Dr",
  icon: "rooms_dr.svg",
  type: "Dr",
  location: build-B
}]

我想要的是根据位置和类型过滤这些数据,并找到结果计数,比如每个位置有多少护士/医生:

$scope.items = [
{
count:2
  icon: "rooms_nurse.svg",
  type: "nurse",
  location: build-A
},
{
  count:2
  icon: "rooms_dr.svg",
  type: "Dr",
  location: build-B
},
 ......    
]

我知道有一个过滤服务,但它只适用于一个属性

   $scope.content = $filter('countBy')($scope.items,'type');

有人能帮我吗?

您可以简单地创建一个自定义过滤器:

angular.module('filterMore', []).
  filter('countBy', function() {
    return function(type,location) {
      var out = [];
      // Filter logic here, adding matches to the out var.
      return out;
    }
  });