Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping array data according to a id

With a API call i'm receiving a response like below

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

]

As you can see first two records consist with the same group. I want to group these data according to the group. So for example it should look like this

[
  {
    groupId: "18",
    groupName : "Ali",
    stations : [
                 {
                  stationId: "10",
                  name: "Jinbaolai"
                 },
                 {
                  stationId: "13",
                  name: "Stack"
                 }
               ]

  },
  {
    groupId: "19",
    groupName : "Baba",
    stations : [
                 {
                  stationId: "20",
                  name: "Overflow"
                 },
               ]

  }

]

I want to do the grouping logic in my reducer where i also set the full data array that is shown in the beginning of the question.

            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
            }

How can i do this? I tried something using filter but not successful.

like image 291
CraZyDroiD Avatar asked Sep 12 '26 16:09

CraZyDroiD


1 Answers

The best way to do this is to use Array.prototype.reduce()

Reduce is an aggregating function where you put in an array of something and get a single vaule back.

There may be a starting value as last parameter like I used {}. The signature is reduce(fn, startingValue) where fn is a function taking two parameters aggregate and currentValue where you return the aggregate in the end.

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;
  }, {}))

}

Here is the fiddle

like image 79
Thomas Junk Avatar answered Sep 15 '26 06:09

Thomas Junk