How to choose from a nested json array

I have JSON, I get from the API as:

 $scope.data = [{
        "primary": "white",
        "sub": ["white 1", "white 2", "white 3"]

    },{
        "primary": "black",
       "sub": ["black 1", "black 2", "black 3"]

    }];

I want to group primary and selectable values: Sub

like:

**White**
   white 1
   white 2
   white 3
**Black**
   Black 1
   Black 2
   Black 3

I can group the primary key, but I cannot get the internal values. Here is what I have:

<div ng-controller="myCtrl">
    <select
        ng-model="myOption"
        ng-options="val.primary group by value.primary for value in data">

    </select>
    <div>
        ng-model value: {{myOption}}
    </div>
</div>

(here is my fiddle: http://jsfiddle.net/jm6of9bu/648/ )

+4
source share
4 answers

Finally, I can solve this with a little tweaking:

Here is what I tried

<div ng-controller="myCtrl">
<select ng-model="myOption" ng-options="value.sub group by value.primary for value in data2">

And I flattened my nested JSON as:

function myCtrl($scope) {
   datas = [{
        "primary": "white",
        "sub": ["white 1", "white 2", "white 3"]

    },{
        "primary": "black",
       "sub": ["black 1", "black 2", "black 3"]
    }];
   $scope.data2 = [];
   datas.forEach(function(entry){
     entry.sub.forEach(function(sub){
        $scope.data2.push({primary:entry.primary,sub:sub})
    });
 });
};

Here is the fiddle: http://jsfiddle.net/jm6of9bu/658/

0
source

You need to smooth the array. Even with group by, it ng-optionsworks on a flat array:

$scope.data2 = [
    {primary: "white", sub: "white1"},
    {primary: "white", sub: "white2"},
    {primary: "white", sub: "white3"},
    {primary: "black", sub: "black1"},
    {primary: "black", sub: "black2"},
    {primary: "black", sub: "black3"},
    ];

Then you could do:

<select
    ng-model="myOption"
    ng-options="value as value.sub group by value.primary for value in data2">

</select>

​​ value - , {primary: "white", sub: "white2"}. "white2", "black3" ..... :

ng-options="value.sub as value.sub group by value.primary for value in data2"
+3

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) { 

$scope.data = [{
        "primary": "white",
        "sub": ["white 1", "white 2", "white 3"]

    },{
        "primary": "black",
       "sub": ["black 1", "black 2", "black 3"]

    }];


$scope.dataList=[];
$scope.data.forEach(function(value,index){
    value.sub.forEach(function(temp,index){    
    $scope.dataList.push({
      'groupType':value.primary,
      'value':temp
      });
    });    
});
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp' ng-controller="myCtrl">
    <select
        ng-model="myOption"
        ng-options="data.value as data.value group by data.groupType for data in dataList">

    </select>
    <div>
        ng-model value: {{myOption}}
    </div>
</div>
Hide result
0

, , ,

If you want the sub-values ​​to display the change, val.primary to val.sub before grouping

<div ng-controller="myCtrl">
    <select
        ng-model="myOption"
        ng-options="val.sub group by value.primary for value in datas">

    </select>
    <div>
        ng-model value: {{myOption}}
    </div>
</div>
-4
source

Source: https://habr.com/ru/post/1598095/


All Articles