Я хочу сгруппировать свои данные по нескольким полям. Итак, я добавил данные и ожидаемый результат.
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')
Я хочу сгруппировать эти скусы по нескольким параметрам, таким как Цвет и Размер, используя loda sh.
По 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
}
}
]
},
....
]
Спасибо.