有没有一种方法可以通过Java中的lodash按多个字段对数据分组

时间:2020-02-09 18:26:29

标签: javascript angular typescript lodash

我想按多个字段对数据分组。因此,我添加了数据和预期结果。

const skus = [
  {
    id: 1,
    customAttributes: {
      Color: 'Red',
      Size: 'S',
      Season: 2019
    }
  },
  {
    id: 2,
    customAttributes: {
      Color: 'Red',
      Size: 'S',
      Season: 2020
    }
  },
  {
    id: 3,
    customAttributes: {
      Color: 'Red',
      Size: 'M',
      Season: 2019
    }
  },
  {
    id: 4,
    customAttributes: {
      Color: 'Red',
      Size: 'M',
      Season: 2020
    }
  },
  {
    id: 5,
    customAttributes: {
      Color: 'Red',
      Size: 'L',
      Season: 2019
    }
  },
  {
    id: 6,
    customAttributes: {
      Color: 'Red',
      Size: 'L',
      Season: 2020
    }
  },
  {
    id: 7,
    customAttributes: {
      Color: 'Green',
      Size: 'S',
      Season: 2019
    }
  },
  {
    id: 8,
    customAttributes: {
      Color: 'Green',
      Size: 'S',
      Season: 2020
    }
  },
  {
    id: 9,
    customAttributes: {
      Color: 'Green',
      Size: 'M',
      Season: 2019
    }
  },
  {
    id: 10,
    customAttributes: {
      Color: 'Green',
      Size: 'M',
      Season: 2020
    }
  },
  {
    id: 11,
    customAttributes: {
      Color: 'Green',
      Size: 'L',
      Season: 2019
    }
  },
  {
    id: 12,
    customAttributes: {
      Color: 'Green',
      Size: 'L',
      Season: 2020
    }
  }
];

console.log( // groupBy is working by 1 parameter, but I don't know to make it multiple parameter
  _.chain(skus)
    .map(sku => sku.customAttributes)
    .groupBy('Color')
    .map((value, key) => ({ title: key, skus: value }))
    .value()
);

// Just like: groupBy('Color', 'Size')

我想使用lodash将这些草图按多个参数分组,就像颜色和大小一样。

通过1个参数,它运行良好。但是当有多个参数时,我做不到。

预期结果是:

[
    {
        title: 'Red / S', // Color / Size
        skus: [
            {
                id: 1,
                customAttributes: {
                  Color: 'Red',
                  Size: 'S',
                  Season: 2019
                }
            },
            {
                id: 2,
                customAttributes: {
                  Color: 'Red',
                  Size: 'S',
                  Season: 2020
                }
            }
        ]
    },
    {
        title: 'Red / M', // Color / Size
        skus: [
            {
                id: 3,
                customAttributes: {
                  Color: 'Red',
                  Size: 'M',
                  Season: 2019
                }
            },
            {
                id: 4,
                customAttributes: {
                  Color: 'Red',
                  Size: 'M',
                  Season: 2020
                }
            }
        ]
    },
    ....
]

谢谢。

1 个答案:

答案 0 :(得分:1)

将地图删除到<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1040.474px" height="209.154px" viewBox="0 0 1040.474 209.154" enable-background="new 0 0 1040.474 209.154" xml:space="preserve"> <g opacity="0.4"> <path fill="#FFFFFF" d="M6.336,27.021h78.367v6.485l-20.269,2.433l53.506,134.573l33.778-84.852l-20.267-49.721l-19.998-2.433 v-6.485h78.365v6.485l-21.348,2.433l53.504,134.573l47.561-129.979l-23.781-7.026v-6.485h56.478v6.485l-20.538,7.298 l-63.233,168.35h-16.213l-44.586-108.631l 43.236,108.631H98.212L27.145,35.939L6.336,33.507V27.021z"/> </g> </svg> ,因为您希望对象中包含customAttributes的对象。将函数传递给id,然后返回一个字符串,其中包含要用作组键的字段。

_.groupBy()
const skus = [{"id":1,"customAttributes":{"Color":"Red","Size":"S","Season":2019}},{"id":2,"customAttributes":{"Color":"Red","Size":"S","Season":2020}},{"id":3,"customAttributes":{"Color":"Red","Size":"M","Season":2019}},{"id":4,"customAttributes":{"Color":"Red","Size":"M","Season":2020}},{"id":5,"customAttributes":{"Color":"Red","Size":"L","Season":2019}},{"id":6,"customAttributes":{"Color":"Red","Size":"L","Season":2020}},{"id":7,"customAttributes":{"Color":"Green","Size":"S","Season":2019}},{"id":8,"customAttributes":{"Color":"Green","Size":"S","Season":2020}},{"id":9,"customAttributes":{"Color":"Green","Size":"M","Season":2019}},{"id":10,"customAttributes":{"Color":"Green","Size":"M","Season":2020}},{"id":11,"customAttributes":{"Color":"Green","Size":"L","Season":2019}},{"id":12,"customAttributes":{"Color":"Green","Size":"L","Season":2020}}];

const result = _(skus)
  .groupBy(({ customAttributes: a }) => `${a.Color} / ${a.Size}`)
  .map((skus, title) => ({ title, skus }))
  .value();
  
console.log(result);