我想按多个字段对我的数据进行分组。因此,我添加了数据和预期结果。
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 按多个参数(例如“颜色”和“大小”)对这些 sku 进行分组。
通过 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
}
}
]
},
....
]
谢谢。
最佳答案
删除到 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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
关于javascript - 有没有办法在Javascript中通过lodash按多个字段对数据进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60139882/