将数组数组转换为对象数组

时间:2014-12-11 10:17:19

标签: javascript arrays angularjs

我有如下所示的数据,我正在使用AngularJS。要访问这些程序,我会在模板中userData.programs。但是,我无法获得与用户关联的服务。 userData.programs.services不起作用。

var userData = 
[  
  {  
    "role":"1",
    "last_name":"Doe",
    "id":1,
    "programs":[  
      {  
        "name":"Program 1",
        "id":"PR1",
        "services":[  
          {  
            "name":"Service 1",
            "id":"SVC1"
          },
          {  
            "name":"Service 2",
            "id":"SVC2"
          },
          {  
            "name":"Service 3",
            "id":"SVC3"
          }
        ]
      },
      {  
        "name":"Program 2",
        "id":"PR4",
        "services":[  
          {  
            "name":"Service 12",
            "id":"SVC12"
          },
          {  
            "name":"Service 14",
            "id":"SVC14"
          },
          {  
            "name":"Service 15",
            "id":"SVC15"
          }
        ]
      }
    ],
    "first_name":"John",
    "email":"user@gmail.com",
    "username":"user"
  }
]

要访问这些服务,我试图操纵JS,如下所示:

 var userServices = [];
 for (var i=0; i < userData.programs.length ; i++) {
     userServices.push(userData.programs[i].services);
 }

但这会产生一个数组数组,即[Array[3], Array[3]],每个数组包含3个对象。我也无法在我的AngularJS模板中访问它。

我有没有办法将此[Array[3], Array[3]]转换为[Object, Object],还是可以直接从我的模板services访问userData

编辑:同时放入我的模板

<select id="authorized-programs" kendo-multi-select="" k-option-label="'Select Programs...'" k-data-text-field="'name'" k-data-value-field="'id'" k-data-source="userData.programs" k-ng-model="userProgramIds" k-on-change="change(kendoEvent)">
</select> //This works perfectly as expected

<select id="authorized-services" kendo-multi-select="" k-option-label="'Select Services...'" k-data-text-field="'name'" k-data-value-field="'id'" k-data-source="???????" k-ng-model="userServiceIds" k-on-change="change(kendoEvent)">
</select> //I need the equivalent data source for services as well. 

0 个答案:

没有答案