javascript - How group objects in array in array from ng-repeat with filter? -


how group objects in array in array ng-repeat filter ?

i have array objects, , group objects countries.

sample : have result :

free : australia, india, united states   pay : australia not pay : australia, india 

from :

{ "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"             }         ]     } ] } 

i tried code :

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

this might better done in controller - cause you'll have different data structure:

$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);     }); }); 

now you'll have each offer object in key name of code, make view:

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

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -