从json动态创建一个数组

时间:2017-07-13 13:23:18

标签: angularjs json list casting

假设我有一个REST,它让我回到了这个json格式:

{
  "data": {
    "result": [
      {
        "dispatch_type": "PDO",
        "distribution_code": "foo"
      },
       {
        "dispatch_type": "PNO",
        "distribution_code": "foo"
      }
    ]
  },
  "errors": []
}

并假设我想在以这种方式声明的select输入中添加所有dispatch_type:

 <select ng-model="dispatchType"
                        ng-options="item as (item | uppercase) for item in searchFilterDispatcher.dispatchType"
                        class="form-control" id="dispatchType">
                        <option value="">*</option>
                    </select>

用这个js:

DispatcherFilterFactory.paramsMock().then(function(response) {
                            var result = response.data.result;
                            $scope.searchFilterDispatcher.dispatchType = result.dispatch_type ;

                        });

我应该如何设置将对象解析为数组的循环,以便我可以使用select?

1 个答案:

答案 0 :(得分:1)

获取"dispatch_type"

的数组
DispatcherFilterFactory.paramsMock().then(function(response) {
  var result = {};
  result.dispatch_type = response.data.result.map(function (row) {
    return row.dispatch_type;
  });
  $scope.searchFilterDispatcher.dispatchType = result.dispatch_type;
});