Javascript 将对象转换为typescript中的列表列表
我正在从Angular 5进行API调用,响应格式如下Javascript 将对象转换为typescript中的列表列表,javascript,angular,typescript,angular6,angular5,Javascript,Angular,Typescript,Angular6,Angular5,我正在从Angular 5进行API调用,响应格式如下 { "metadata":{ "lastSyncTime":"2000-11-21T16:07:53", "dataFromDB":true }, "allocationReports":[ { "allocatedUserCount":100,
{ "metadata":{
"lastSyncTime":"2000-11-21T16:07:53",
"dataFromDB":true
},
"allocationReports":[ {
"allocatedUserCount":100,
"healthGoalName":"Eat Healthier"
},
{
"allocatedUserCount":130,
"healthGoalName":"Feel Happier"
},
{
"allocatedUserCount":150,
"healthGoalName":"Quit Smoking"
}
],
"overall":{
"usersWithGoalCount":0,
"registeredCount":500,
"eligibleCount":280
}
}
我需要将这些数据转换为列表列表(或数组数组),以便在此数据上绘制多个圆环图。我尝试了多种方法,比如使用.map,但是在单个列表中获取所有值。如何以以下格式构建数据
{ "metadata":{
"lastSyncTime":"2000-11-21T16:07:53",
"dataFromDB":true
},
"allocationReports":[ {
"allocatedUserCount":100,
"healthGoalName":"Eat Healthier"
},
{
"allocatedUserCount":130,
"healthGoalName":"Feel Happier"
},
{
"allocatedUserCount":150,
"healthGoalName":"Quit Smoking"
}
],
"overall":{
"usersWithGoalCount":0,
"registeredCount":500,
"eligibleCount":280
}
}
所需格式为:
[
[
{ "x": "Eat Healthier", "y": 100 },
{ "x": "Total registered", "y": 500 }
],
[
{ "x": "Feel Happier", "y": 130 },
{ "x": "Total registered", "y": 500 }
],
[
{ "x": "Quit Smoking", "y": 150 },
{ "x": "Total registered", "y": 500 }
]
]
总的注册值将来自总体。registeredCount
试试这个
ob={“metadata”:{“lastSyncTime”:“2000-11-21T16:07:53”,“dataFromDB”:true},“分配报告”:[{“allocatedUserCount”:100,“healthGoalName”:“吃得更健康”},{“allocatedUserCount”:130,“healthGoalName”:“感觉更快乐”},{“allocatedUserCount”:150,“healthGoalName”:“戒烟”}],“总体”:{“usersWithGoalCount”:0,“registeredCount”:500,“eligibleCount”:280}
r=ob.allocationReports.map(o=>{return[{x:o.healthGoalName,y:o.allocatedUserCount},{x:“已注册总数”,y:ob.Total.registeredCount}])
console.log(r)
如果您能提供帮助,请再问一个问题。我们可以按“allocatedUserCount”的升序对结果数组进行排序吗。