如何使用 Ramda 规范化 Api 响应
How to normalize Api response using Ramda
如何使用 ramda 规范化此 api 响应:
appointmentsConfig: Object
isEnable: true
isReadonly: false
reminderInAdvanceMinutes: 1440
__proto__: Object
tasksConfigs: Array(4)
0: Object
isEnable: true
isReadonly: false
reminderInAdvanceMinutes: 15
reminderRepeatIntervalMinutes: 10
remindersNumber: 0
shouldRepeatUntilTaskDone: true
taskTypeGroupAlias: "Medications"
taskTypes: Array(1)
__proto__: Object
1: Object
isEnable: true
isReadonly: false
reminderInAdvanceMinutes: 15
reminderRepeatIntervalMinutes: 10
remindersNumber: 0
shouldRepeatUntilTaskDone: true
taskTypeGroupAlias: "Measurements"
taskTypes: Array(2)
__proto__: Object
...etc.
给这个对象:
{
"Medications": {
"taskTypeGroupAlias": "Medications",
"taskTypes": [
"MedicationTask"
],
"isReadonly": false,
"isEnable": true,
"shouldRepeatUntilTaskDone": true,
"remindersNumber": 0,
"reminderRepeatIntervalMinutes": 10,
"reminderInAdvanceMinutes": 15
},
...and the same Measurements, Surveys, Tasks objects
...and one different object below
"Visits": {
"isReadonly": false,
"isEnable": true,
"reminderInAdvanceMinutes": 1440
}
}
访问 - 它是一个对象 appointmentsConfig。
我想我必须使用 reduceBy 方法,但我无法解决此任务。
如果 tasksConfigs
每个 taskTypeGroupAlias
类型都有一个项目,您可以使用 R.indexBy:
R.indexBy(R.prop('taskTypeGroupAlias'), response.tasksConfigs);
但是,如果每个 taskTypeGroupAlias
可能出现多次,您应该考虑 R.groupBy:
R.groupBy(R.prop('taskTypeGroupAlias'), response.tasksConfigs);
这将 return 一个数组对象,其中每个数组包含具有相同 taskTypeGroupAlias
:
的对象
{
Medications: [],
Measurements: [],
Surveys: [],
Tasks: [],
Visits: [],
}
如何使用 ramda 规范化此 api 响应:
appointmentsConfig: Object
isEnable: true
isReadonly: false
reminderInAdvanceMinutes: 1440
__proto__: Object
tasksConfigs: Array(4)
0: Object
isEnable: true
isReadonly: false
reminderInAdvanceMinutes: 15
reminderRepeatIntervalMinutes: 10
remindersNumber: 0
shouldRepeatUntilTaskDone: true
taskTypeGroupAlias: "Medications"
taskTypes: Array(1)
__proto__: Object
1: Object
isEnable: true
isReadonly: false
reminderInAdvanceMinutes: 15
reminderRepeatIntervalMinutes: 10
remindersNumber: 0
shouldRepeatUntilTaskDone: true
taskTypeGroupAlias: "Measurements"
taskTypes: Array(2)
__proto__: Object
...etc.
给这个对象:
{
"Medications": {
"taskTypeGroupAlias": "Medications",
"taskTypes": [
"MedicationTask"
],
"isReadonly": false,
"isEnable": true,
"shouldRepeatUntilTaskDone": true,
"remindersNumber": 0,
"reminderRepeatIntervalMinutes": 10,
"reminderInAdvanceMinutes": 15
},
...and the same Measurements, Surveys, Tasks objects
...and one different object below
"Visits": {
"isReadonly": false,
"isEnable": true,
"reminderInAdvanceMinutes": 1440
}
}
访问 - 它是一个对象 appointmentsConfig。
我想我必须使用 reduceBy 方法,但我无法解决此任务。
如果 tasksConfigs
每个 taskTypeGroupAlias
类型都有一个项目,您可以使用 R.indexBy:
R.indexBy(R.prop('taskTypeGroupAlias'), response.tasksConfigs);
但是,如果每个 taskTypeGroupAlias
可能出现多次,您应该考虑 R.groupBy:
R.groupBy(R.prop('taskTypeGroupAlias'), response.tasksConfigs);
这将 return 一个数组对象,其中每个数组包含具有相同 taskTypeGroupAlias
:
{
Medications: [],
Measurements: [],
Surveys: [],
Tasks: [],
Visits: [],
}