将对象转换为打字稿中的列表列表

Transform an object to list of lists in typescript

我正在从 Angular 5 拨打 API 电话,回复格式如下。

{   "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,但在单个列表中获取所有值。如何构建以下格式的数据。

要求的格式是:

[ 
 [
  { "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 } 
 ]
]

注册总价值将来自overall.registeredCount

试试这个

ob={"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 } }

r=ob.allocationReports.map(o=>{return [{x:o.healthGoalName,y:o.allocatedUserCount},{x: "Total registered", y: ob.overall.registeredCount }]})

console.log(r)