如何将Json数据放入变量Angular 2中?

时间:2017-07-14 09:16:18

标签: javascript arrays angular typescript

enter image description here

params.value中,我有3个数组,其中我需要将Names放在一个变量中。我尝试通过ForEach做但我做不到,出了什么问题?

var roles = params.forEach(x => params.value.Name);
return roles;

{"Object":[{"TerritoryId":1,"TerritoryName":"Russia","FirmPieRotationRuleId":1,"FirmPieWorkedYears":0,"FirmPieCoolOff":5,"FirmPieRoles":[{"TeamRoleId":1,"Name":"Engagement Leader","MinCount":1,"MaxCount":1,"SortOrder":0},{"TeamRoleId":2,"Name":"Signing Partner","MinCount":1,"MaxCount":1,"SortOrder":0},{"TeamRoleId":3,"Name":"Engagement Manager","MinCount":1,"MaxCount":1,"SortOrder":0}],"StatutoryPieRotationRuleId":null,"StatutoryPieWorkedYears":null,"StatutoryPieCoolOff":null,"StatutoryPieRoles":[],"NonPieRotationRuleId":null,"NonPieWorkedYears":null,"NonPieCoolOff":null,"NonPieRoles":[]}],"Code":0,"ErrorMessage":null,"Errors":[]}

2 个答案:

答案 0 :(得分:2)

Array#forEach不会返回任何内容。请尝试使用Array#map

var roles = params.map(x => params.value.Name);

答案 1 :(得分:0)

value是数组。试试这个:

var roles = params.value.map(val => val.Name)