Group objects by nested child objects of array in array

Viewed 35

I have data received from a gateway and a beacon. I want to group my data by beacon(mac address) which contains gateways and its own array

{
  "data": [
    {
      "timestamp": "2022-09-16T08:00:00.000Z",
      "mac": "3D:84:CB:C2:05:14",
      "beacons": [
        {
          "mac": "AC233FE81A09",
          "rssi": -14,
          "timestamp": "2022-09-16T08:00:01.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        }
      ]
    },
    {
      "timestamp": "2022-09-16T08:00:10.000Z",
      "mac": "3D:84:CB:C2:05:14",
      "beacons": [
        {
          "mac": "AC233FE81A09",
          "rssi": -14,
          "timestamp": "2022-09-16T08:00:11.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        }
      ]
    }
  ]
}

I used this code and it works.

const groupGatewayByBeacon = (gateways) => {
  // reduce down each gateway into a beacon map
  const gatewayGroupedByBeacon = gateways.reduce((beacons, gateway) => {
    delete gateway.timestamp;
    // insert the current gateway into each of the beacons it contains
    gateway.beacons.forEach((beacon) => {
      // if the beacon exists in the map, we just need to append the current gateway
      if (beacon.mac in beacons)
        beacons[beacon.mac].map((beacon) => {
          beacon.beacons.push(beacon);
        });
      // otherwise, create a new beacon object with the current gateway
      else beacons[beacon.mac] = [{ ...gateway, beacons: [beacon] }];
    });
    return beacons;
  }, []);

  return gatewayGroupedByBeacon;
};

Output:

{
AC233FE81A09: {
    mac: '3D:84:CB:C2:05:14',
    beacons: [
        {
          "mac": "AC233FE81A09",
          "rssi": -14,
          "timestamp": "2022-09-16T08:00:01.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        },
        {
          "mac": "AC233FE81A09",
          "rssi": -14,
          "timestamp": "2022-09-16T08:00:11.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        }
     ]
   }
}

But when data with multiple gateways and multiple beacons.The above code is not working.

{
  "data": [
    {
      "timestamp": "2022-09-16T08:00:00.000Z",
      "mac": "3D:84:CB:C2:05:14",
      "beacons": [
        {
          "mac": "AC233FE81A09",
          "rssi": -14,
          "timestamp": "2022-09-16T08:00:01.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        }
      ]
    },
    {
      "timestamp": "2022-09-16T08:05:00.000Z",
      "mac": "3D:84:CB:C2:05:14",
      "beacons": [
        {
          "mac": "AC233FE81A05",
          "rssi": -14,
          "timestamp": "2022-09-16T08:05:01.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        },
        {
          "mac": "AC233FE81A09",
          "rssi": -17,
          "timestamp": "2022-09-16T08:05:03.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        }
      ]
    },
    {
      "timestamp": "2022-09-16T08:00:00.000Z",
      "mac": "3D:84:CB:C2:05:20",
      "beacons": [
        {
          "mac": "AC233FE81A05",
          "rssi": -14,
          "timestamp": "2022-09-16T08:00:11.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        },
        {
          "mac": "AC233FE81A09",
          "rssi": -16,
          "timestamp": "2022-09-16T08:00:12.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        }
      ]
    }
  ]
}

Desired output:

{
  AC233FE81A09: [
     {
      mac: '3D:84:CB:C2:05:14',
      beacons: [
          {
            "mac": "AC233FE81A09",
            "rssi": -17,
            "timestamp": "2022-09-16T08:05:03.000Z",
            "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
            "ibeaconTxPower": -59
          },
          {
            "mac": "AC233FE81A09",
            "rssi": -14,
            "timestamp": "2022-09-16T08:00:01.000Z",
            "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
            "ibeaconTxPower": -59
          }
       ]
     },{
      mac: '3D:84:CB:C2:05:20',
      beacons: [
        {
          "mac": "AC233FE81A09",
          "rssi": -16,
          "timestamp": "2022-09-16T08:00:12.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        },
       ]
     }
    ],
  AC233FE81A05: [
      {
      mac: '3D:84:CB:C2:05:14',
      beacons: [
        {
          "mac": "AC233FE81A05",
          "rssi": -14,
          "timestamp": "2022-09-16T08:05:01.000Z",
          "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
          "ibeaconTxPower": -59
        }
       ]
      },
      {
        mac: '3D:84:CB:C2:05:20',
        beacons: [
          {
            "mac": "AC233FE81A05",
            "rssi": -14,
            "timestamp": "2022-09-16T08:00:11.000Z",
            "ibeaconUuid": "E2C56DB5DFFB48D2B060D0F5A71096E0",
            "ibeaconTxPower": -59
          }
         ]
      }
    ]
  }

I really appreciate any help.

0 Answers
Related