在Javascript Angular 6中将字符串转换为数组

时间:2019-02-11 08:44:54

标签: javascript arrays angular multidimensional-array

我有一个动态生成的数组,该数组从SELECT AEC.gwd_people.id_people, AEC.gwd_people.uid_people, AEC.gwd_people.cod_people, AEC.gwd_people.name_people, AEC.gwd_people.surname_people, AEC.gwd_people.email, AEC.gwd_people.people_status, AEC.gwd_people.people_type, AEC.gwd_people.facility_reference, AEC.gwd_people.sc_id_sap, AEC.gwd_people.c_id_sap, AEC.gwd_people.descr_people, AEC.gwd_people.cod_sector, AEC.gwd_people.descr_sector, AEC.gwd_people.cod_org_sector, AEC.gwd_people.descr_org_sector, AEC.gwd_people.cod_company, AEC.gwd_people.descr_company, AEC.gwd_people.cod_company_sap, AEC.gwd_people.cod_department, AEC.gwd_department.descr_department, AEC.gwd_people.cod_subdepartment, AEC.gwd_people.descr_subdepartment, AEC.gwd_people.cod_cdc, AEC.gwd_cost_center.descr_cdc, AEC.gwd_people.cod_category_job, AEC.gwd_people.descr_category_job, AEC.gwd_people.cod_people_job, AEC.gwd_people.descr_people_job, AEC.gwd_people.cod_position, AEC.gwd_people.descr_position, AEC.gwd_people.uohr, AEC.gwd_people.qual_contract, AEC.gwd_people.level_position, AEC.gwd_people.cod_manager, AEC.gwd_people.cod_validator, AEC.gwd_people.cod_country, AEC.gwd_people.descr_country, AEC.gwd_people.cod_region_area, AEC.gwd_people.descr_region_area, AEC.gwd_people.descr_city, AEC.gwd_people.descr_site, AEC.gwd_people.address_1, AEC.gwd_people.address_2, AEC.gwd_people.descr_building, AEC.gwd_people.descr_room, AEC.gwd_people.validity_date, AEC.aec_workstation.cod_workstation, AEC.aec_workstation.geometry, AEC.aec_workstation.drawing, AEC.gwd_people.tax_code, AEC.gwd_people.phone_1, AEC.gwd_people.phone_2, AEC.gwd_people.phone_3, AEC.gwd_people.phone_4, AEC.gwd_people.ext_email_1, AEC.gwd_people.flagvip, AEC.gwd_people.hiring_date, AEC.gwd_people.cease_date, AEC.gwd_people.cid_resp_liv_1, AEC.gwd_people.cid_resp_liv_2, AEC.gwd_people.id_resp, AEC.gwd_people.descr_resp, AEC.gwd_people.id_ref, AEC.gwd_people.descr_ref, AEC.gwd_people.descr_ext_people, AEC.gwd_people.ext_email_2, AEC.gwd_people.descr_sede, (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NULL THEN AEC.gwd_people.idplan ELSE NULL END) AS idplan, (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NOT NULL THEN SUBSTRING(AEC.aec_workstation.cod_workstation, 5, 7) ELSE NULL END) AS idplan_wrkst, (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NULL THEN AEC.view_iam_r_unitp_building.IDEDIFICIO ELSE NULL END) AS cod_building, (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NOT NULL THEN SUBSTRING(AEC.aec_workstation.cod_workstation, 5, 3) ELSE NULL END) AS cod_building_wrkst, (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NOT NULL THEN AEC.aec_workstation.id_room ELSE NULL END) AS id_room_wrkst, (CASE WHEN AEC.aec_r_workstation_people.cod_people IS NOT NULL THEN AEC.aec_workstation.id_room ELSE NULL END) AS id_room_wrkst2 FROM AEC.gwd_people LEFT OUTER JOIN AEC.view_iam_r_unitp_building ON AEC.view_iam_r_unitp_building.IDUNITPROD = AEC.gwd_people.cod_sector LEFT OUTER JOIN AEC.aec_r_workstation_people ON AEC.gwd_people.cod_people = AEC.aec_r_workstation_people.cod_people LEFT OUTER JOIN AEC.aec_workstation ON AEC.aec_workstation.cod_workstation = AEC.aec_r_workstation_people.cod_workstation LEFT OUTER JOIN AEC.gwd_department ON AEC.gwd_department.cod_department = AEC.gwd_people.cod_department LEFT OUTER JOIN AEC.gwd_cost_center ON AEC.gwd_cost_center.cod_cost_center = AEC.gwd_people.cod_cdc 中显示:

console.log(temp)

我的代码:

[{user_id: "test@random.com", status: "Idle"}], 
[{user_id: "whatever@random.com", status: "Busy"}]

如何将阵列的格式更改为类似于下面的阵列?

this.scheduleService.getShiftSchedule().subscribe((temp)=>{
  this.api = temp;
  console.log(temp);
})

3 个答案:

答案 0 :(得分:2)

您可以通过获取所需键及其id的数组来映射对象。

var data = [{ user_id: "test@random.com", status: "Idle" }, { user_id: "whatever@random.com", status: "Busy" }],
    ids = [['user_id', 1], ['status', 2]],
    result = data.map(o => ids.map(([key, id]) => ({ id, content: o[key] })));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

var arr = [1,2,3,4,5,6,7,8,9]

var newArr = [];
while(arr.length) newArr.push(arr.splice(0,3));

console.log(newArr)

您可以尝试这样

答案 2 :(得分:0)

var array = JSON.parse(“ [” +字符串+“]”);