简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM