通过 API 调用我收到了如下响应。
我希望在我的reducer中实现分组逻辑,同时设置显示在问题开头的完整数据数组。
[
{
stationId: "10"
name: "Jinbaolai"
group: {id: "18", stationGroupName: "Ali"}
},
{
stationId: "13"
name: "Stack"
group: {id: "18", stationGroupName: "Ali"}
},
{
stationId: "20"
name: "Overflow"
group: {id: "19", stationGroupName: "Baba"}
}
]
如您所见,前两条记录属于同一组。我希望将这些数据根据组分组。例如,它应该是这样的:
[
{
groupId: "18",
groupName : "Ali",
stations : [
{
stationId: "10",
name: "Jinbaolai"
},
{
stationId: "13",
name: "Stack"
}
]
},
{
groupId: "19",
groupName : "Baba",
stations : [
{
stationId: "20",
name: "Overflow"
},
]
}
]
我希望在我的reducer中实现分组逻辑,同时设置显示在问题开头的完整数据数组。
case EVC_SUCCESS:
return {
...state,
chargingStations: action.evcData.chargingStations,
chargingStationGroups: //This is where my logic should go. ('action.evcData.chargingStations' is the initial data array)
tableLoading: false
}
我该怎么做?我尝试使用 filter
但没有成功。