简体   繁体   English

Lodash 对对象数组中的项目进行分组

[英]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我试过 _groupBy 并没有得到预期的结果

    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.预期结果应包含最多 2 种组合(5000 和 6000)的 tOffice,band 是 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我尝试了一些 ES6 方式,发现 lodash 方法将是一个不错的选择,但我不知道

This is a vanila js solution.这是一个 vanila js 解决方案。 You could do something like this using reduce and Object.values你可以使用reduceObject.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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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