AngularJS按唯一属性值过滤并获取过滤后的计数

אVי*_*אVי 3 javascript angularjs

我有以下数组:

items: [
{
    id: "0",
    name: "????? ?????",
    icon: "rooms_salon.svg",
    type: "scenario",
    status: 1
},
{
    id: "1",
    name: "????? ?????",
    icon: "rooms_salon.svg",
    type: "scenario",
    status: 1
},
{
    id: "0",
    name: "???? ????",
    icon: "rooms_salon.svg",
    type: "heater",
    status: 0
},
{
    id: "1",
    name: "???? ????",
    icon: "rooms_salon.svg",
    type: "ac",
    status: 0
}]
Run Code Online (Sandbox Code Playgroud)

我需要过滤它,所以我只获得唯一的item.type和每种类型的原始项目数量.

我需要的坚定:

items: [
    {
        type: "scenario",
        amount: 2
    },
    {
        type: "heater",
        amount: 1
    },
    {
        type: "ac",
        amount: 1
    }
]
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?

PO:我需要在控制器中过滤它,而不是在ng-repeat中过滤它.

谢谢分配

阿维

boi*_*iil 11

您可以使用该angular-filter模块对项目进行分组:

$scope.items = [
{
    id: "0",
    name: "????? ?????",
    icon: "rooms_salon.svg",
    type: "scenario",
    status: 1
},
{
    id: "1",
    name: "????? ?????",
    icon: "rooms_salon.svg",
    type: "scenario",
    status: 1
},
{
    id: "0",
    name: "???? ????",
    icon: "rooms_salon.svg",
    type: "heater",
    status: 0
},
{
    id: "1",
    name: "???? ????",
    icon: "rooms_salon.svg",
    type: "ac",
    status: 0
}]

$scope.content = $filter('countBy')($scope.items,'type');
Run Code Online (Sandbox Code Playgroud)

在这里你有一个工作的plunker