Javascript 使用typescript对Angular5中的JSON响应进行排序
我正在从Angular 5进行API调用,响应格式如下Javascript 使用typescript对Angular5中的JSON响应进行排序,javascript,angular,typescript,Javascript,Angular,Typescript,我正在从Angular 5进行API调用,响应格式如下 let ob = { "metadata":{ "lastSyncTime":"2000-11-21T16:07:53", "dataFromDB":true }, "allocationReports":[ { "allocatedUserCount"
let ob = { "metadata":{
"lastSyncTime":"2000-11-21T16:07:53",
"dataFromDB":true
},
"allocationReports":[
{
"allocatedUserCount":130,
"healthGoalName":"Feel Happier"
},
{
"allocatedUserCount":150,
"healthGoalName":"Quit Smoking"
},
{
"allocatedUserCount":100,
"healthGoalName":"Eat Healthier"
}
],
"overall":{
"usersWithGoalCount":0,
"registeredCount":500,
"eligibleCount":280
}
}
我需要将这些数据转换为列表列表(或数组数组),以便在此数据上绘制多个圆环图。我尝试了多种方法,比如使用.map,但是在单个列表中获取所有值。如何以以下格式构建数据
let ob = { "metadata":{
"lastSyncTime":"2000-11-21T16:07:53",
"dataFromDB":true
},
"allocationReports":[
{
"allocatedUserCount":130,
"healthGoalName":"Feel Happier"
},
{
"allocatedUserCount":150,
"healthGoalName":"Quit Smoking"
},
{
"allocatedUserCount":100,
"healthGoalName":"Eat Healthier"
}
],
"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 }
]
]
我已经写了下面的代码
r=ob.allocationReports.map(o=>{return [{x:o.healthGoalName,y:o.allocatedUserCount},{x: "Total registered", y: ob.overall.registeredCount }]})
但是我得到的结果没有分类。我该如何排序。试试这个,它将按升序对它们进行排序
ob={“metadata”:{“lastSyncTime”:“2000-11-21T16:07:53”,“dataFromDB”:true},“分配报告”:[{“分配目标计数”:130,“健康目标名称”:“感觉更快乐”},{“分配目标计数”:100,“健康目标名称”:“吃得更健康”},{“分配目标计数”:150,“健康目标名称”:“戒烟”}],“总体”:{“用户分配目标计数”:0,“registeredCount”:500,“eligibleCount”:280}
r=ob.allocationReports.map(o=>{return[{x:o.healthGoalName,y:o.allocatedUserCount},{x:“已注册总数”,y:ob.Total.registeredCount}]}).sort((a,b)=>a[0].y-b[0].y)
console.log(r)
试试这个,它会按升序对它们进行排序
ob={“metadata”:{“lastSyncTime”:“2000-11-21T16:07:53”,“dataFromDB”:true},“分配报告”:[{“allocatedUserCount”:130,“healthGoalName”:“感觉更快乐”},{“allocatedUserCount”:100,“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}]}).sort((a,b)=>a[0].y-b[0].y)
console.log(r)
这能回答您的问题吗?是的。这很有效。谢谢。这能回答您的问题吗?是的。这很有效。谢谢。