1

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

adiga
  • 34,372
  • 9
  • 61
  • 83
Mithun S
  • 408
  • 8
  • 20
  • [Most efficient method to groupby on a array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-a-array-of-objects) – Andreas Feb 04 '19 at 13:35

1 Answers1

1

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)
adiga
  • 34,372
  • 9
  • 61
  • 83
  • How can i acess r.tOffice instead of r[tOffice]? – Mithun S Feb 04 '19 at 14:24
  • I'm [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) each item in the `reduce` callback. `tOffice` is variable. You need to access it like that – adiga Feb 04 '19 at 14:26
  • How to do this in typescript way. i am getting lot of property issues in Typescript – Mithun S Feb 04 '19 at 14:54