Javascript 如何将数组中的对象从ng中分组并使用筛选器重复?

Javascript 如何将数组中的对象从ng中分组并使用筛选器重复?,javascript,angularjs,Javascript,Angularjs,如何将数组中的对象从ng中分组并使用筛选器重复 我有一个包含对象的数组,我想按这些对象所在的国家进行分组 示例:我希望得到以下结果: Free : Australia, India, United States Pay : Australia Not Pay : Australia, India 发件人: { "id": 1, "offer": [ { "id": 9806, "country": { "name": "Aus

如何将数组中的对象从ng中分组并使用筛选器重复

我有一个包含对象的数组,我想按这些对象所在的国家进行分组

示例:我希望得到以下结果:

Free : Australia, India, United States  
Pay : Australia
Not Pay : Australia, India
发件人:

{
"id": 1,
"offer": [
    {
        "id": 9806,
        "country": {
            "name": "Australia"
        },
        "code_show": [
            {
                "code": "Free"
            },
            {
                "code": "Pay"
            },
            {
                "code": "Not Pay"
            }
        ]
    },
    {
        "id": 9807,
        "country": {
            "name": "India"
        },
        "code_show": [
            {
                "code": "Free"
            },
            {
                "code": "Not Pay"
            }
        ]
    },
    {
        "id": 9808,
        "country": {
            "name": "United States"
        },
        "code_show": [
            {
                "code": "Free"
            }
        ]
    }
],
},
{
"id": 2,
"offer": [
    {
        "id": 9806,
        "country": {
            "name": "Australia"
        },
        "code_show": [
            {
                "code": "Free"
            },
            {
                "code": "Not Pay"
            }
        ]
    },
    {
        "id": 9807,
        "country": {
            "name": "Mexico"
        },
        "code_show": [
            {
                "code": "Free"
            }
        ]
    },
    {
        "id": 9808,
        "country": {
            "name": "United States"
        },
        "code_show": [
            {
                "code": "Free"
            }
        ]
    }
]
}
我尝试了以下代码:

<ul ng-repeat="(key, value) in offer.code_show | groupBy: 'code_show.code'">
  {{ key }}
  <li ng-repeat="country in value">
    : {{ country.name }} 
  </li>
</ul>
    {{key}}
  • :{{country.name}

在控制器中执行此操作可能更好—主要原因是您将拥有不同的数据结构:

$scope.groupedByCode = {};
$scope.offer.forEach(function(obj) {
    obj["code_show"].forEach(function(code) {
        if (!$scope.groupedByCode[code]) {
            $scope.groupedByCode[code] = [];
        }

        $scope.groupedByCode[code].push(obj);
    });
});
现在,您将每个
提供的
对象放在一个带有代码名称的键中,然后创建视图:

<ul ng-repeat="(key, value) in groupedByCode">
    {{ key }}
    <li ng-repeat="country in value">
        : {{ country.country.name }} 
    </li>
</ul>
    {{key}}
  • :{country.country.name}