根据id对数组数据进行分组

Grouping array data according to a id

通过 API 呼叫,我收到如下回复

[
  {
    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 但没有成功。

一个简单的 JS 算法就可以为您做到这一点

const list = [
  {
    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"}
  }

];
const groups = {};

list.forEach((item) => {
  const groupId = item.group.id;
  const group = groups[groupId] || {groupId: groupId, groupName: item.group.stationGroupName, stations: []};

  group.stations.push({stationId: item.stationId, name: item.name});
  groups[groupId] = group;
});

const groupedArray = Object.keys(groups).map((groupId) => groups[groupId]);

console.log(groupedArray); // This will be the output you want

最好的方法是使用 Array.prototype.reduce()

Reduce 是一个聚合函数,您可以在其中放入一个数组并返回一个值。

可能有一个起始值作为最后一个参数,就像我使用的 {}。 签名是 reduce(fn, startingValue),其中 fn 是一个带有两个参数 aggregatecurrentValue 的函数,您最终 return 聚合。

const groupData = (data)=> {

    return Object.values(data.reduce((group,n)=>{
    if (!group[n.group.id]){
        group[n.group.id] = {
      groupId:n.group.id,
      groupName: n.group.stationGroupName,
      stations:[]}
    } 
    group[n.group.id].stations.push({
      stationID: n.stationId,
      name: n.name
    })
    return group;
  }, {}))

}

这是fiddle

我认为链接多个函数会起作用。

const stations = [
  {
    "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"}
  }
]
const groups = _.chain(stations)
.groupBy((station) => { return station.group.id })
.map((values, key) => { 
  return {
    "groupId": _.first(values).group.id,
    "groupName": _.first(values).group.id,
    "stations": _.map(values,(value)=>{ return { "stationId": value.stationId, "name": value.name   } })
  }
})
console.log("groups",groups)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>