Search code examples
javascriptarraysobjectecmascript-6lodash

Lodash to subgroup items in Array of Objects


I have an Array of objects with band property. i would like to group them according to band

I have tried _groupBy and failed to get expected result

    var data=[
  {
    "tOffice": 123,
    "cType": 5000,
    "band": -4877,
    "minCh": 12,
    "maxCh": 20
  },
  {
    "tOffice": 123,
    "cType": 5000,
    "band": -4877,
    "minCh": 1,
    "maxCh": 2
  },
  {
    "tOffice": 123,
    "cType": 5000,
    "band": -4877,
    "minCh": 11,
    "maxCh": 12
  },
  {
    "tOffice": 123,
    "cType": 6000,
    "band": -5877,
    "minCh": 11,
    "maxCh": 12
  },
  {
    "tOffice": 456,
    "cType": 5000,
    "band": -4544,
    "minCh": 12,
    "maxCh": 20
  },
  {
    "tOffice": 456,
    "cType": 5000,
    "band": -4544,
    "minCh": 10,
    "maxCh": 11
  },
  {
    "tOffice": 456,
    "cType": 6000,
    "band": -5211,
    "minCh": 12,
    "maxCh": 20
  },
  {
    "tOffice": 456,
    "cType": 6000,
    "band": -5211,
    "minCh": 12,
    "maxCh": 20
  },
  {
    "tOffice": 456,
    "cType": 6000,
    "band": -5211,
    "minCh": 12,
    "maxCh": 20
  }
];

Expected Output:

var expectedResult = [{
   tOffice: 123,
   bandType: [{
         band: '123-5000',
         minMaxCharge: [{
            minCh: 12,
            maxCh: 20
         }, {
            minCh: 1,
            maxCh: 2
         }, {
            minCh: 11,
            maxCh: 12
         }]
      },
      {
         band: '123-6000',
         minMaxCharge: [{
            minCh: 11,
            maxCh: 12
         }]

      }
   ]
}, {
   tOffice: 456,
   bandType: [{
         band: '456-5000',
         minMaxCharge: [{
            minCh: 12,
            maxCh: 20
         }, {
            minCh: 10,
            maxCh: 11
         }]
      },
      {
         band: '456-6000',
         minMaxCharge: [{
            minCh: 12,
            maxCh: 20
         }, {
            minCh: 12,
            maxCh: 20
         }]
      }
   ]
}]

The expected result should contain a tOffice with max 2 combinations(5000 & 6000), band is a combination of tOffice+cType.

_.chain(data).groupBy("band").map(function(v, i) {
  return {

  }
})

I have tried some ES6 way and found that lodash methods will be a good choice, but i am not aware about that


Solution

  • This is a vanila js solution. You could do something like this using reduce and Object.values

    var data=[{"tOffice":123,"cType":5000,"band":-4877,"minCh":12,"maxCh":20},{"tOffice":123,"cType":5000,"band":-4877,"minCh":1,"maxCh":2},{"tOffice":123,"cType":5000,"band":-4877,"minCh":11,"maxCh":12},{"tOffice":123,"cType":6000,"band":-5877,"minCh":11,"maxCh":12},{"tOffice":456,"cType":5000,"band":-4544,"minCh":12,"maxCh":20},{"tOffice":456,"cType":5000,"band":-4544,"minCh":10,"maxCh":11},{"tOffice":456,"cType":6000,"band":-5211,"minCh":12,"maxCh":20},{"tOffice":456,"cType":6000,"band":-5211,"minCh":12,"maxCh":20},{"tOffice":456,"cType":6000,"band":-5211,"minCh":12,"maxCh":20}];
    
    const merged = data.reduce((r,{tOffice, cType, minCh, maxCh})=>{
      const office = r[tOffice] =  r[tOffice] || {tOffice, bandType:{}};
      const band = `${tOffice}-${cType}`
      
      const nested = office.bandType[band] 
                   = office.bandType[band] || {band, minMaxCharge:[]};
                   
      nested.minMaxCharge.push({minCh, maxCh})
      return r
    },{})
    
    const final = Object.values(merged);
    final.forEach(a => a.bandType = Object.values(a.bandType))
    
    console.log(final)