Я хочу сгруппировать массив объектов по 2 свойствам. Как я могу это сделать?
Я получил этот массив:
[
{
"nome": "Beatrice Mattos",
"apelido": "Beatrice",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T15:00:00",
"Servico": "Corte Masculino"
},
{
"nome": "Davi",
"apelido": "Davi",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T14:00:00",
"Servico": "Corte Masculino"
},
{
"nome": "Kai",
"apelido": "Martins",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T13:30:00",
"Servico": "Barba Completa"
},
{
"nome": "Beatrice Mattos",
"apelido": "Beatrice",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T09:30:00",
"Servico": "Corte Masculino"
},
{
"nome": "Beatrice Mattos",
"apelido": "Beatrice",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T09:00:00",
"Servico": "Alongamento de Cabelo"
},
{
"nome": "Beatrice Mattos",
"apelido": "Beatrice",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T08:00:00",
"Servico": "Corte Masculino"
},
{
"nome": "Estevan Alves",
"apelido": "Estevan",
"dt_extrato": "2020-05-25T00:00:00",
"dt_inicio": "2020-05-25T08:40:00",
"Servico": "Corte Masculino"
},
{
"nome": "Kai",
"apelido": "Martins",
"dt_extrato": "2020-05-25T00:00:00",
"dt_inicio": "2020-05-25T08:30:00",
"Servico": "Corte Masculino"
},
{
"nome": "Estevan Alves",
"apelido": "Estevan",
"dt_extrato": "2020-05-25T00:00:00",
"dt_inicio": "2020-05-25T08:00:00",
"Servico": "Corte Masculino"
}
]
И я хочу преобразовать его в этот объект:
{
"2020-05-26T00:00:00": [{
"Beatrice Mattos": [
{
"nome": "Beatrice Mattos",
"apelido": "Beatrice",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T15:00:00",
"Servico": "Corte Masculino"
},
{
"nome": "Beatrice Mattos",
"apelido": "Beatrice",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T09:30:00",
"Servico": "Corte Masculino"
},
{
"nome": "Beatrice Mattos",
"apelido": "Beatrice",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T09:00:00",
"Servico": "Alongamento de Cabelo"
},
{
"nome": "Beatrice Mattos",
"apelido": "Beatrice",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T08:00:00",
"Servico": "Corte Masculino"
}
],
"Davi": [
{
"nome": "Davi",
"apelido": "Davi",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T14:00:00",
"Servico": "Corte Masculino"
}
],
"Kai": [
{
"nome": "Kai",
"apelido": "Martins",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T13:30:00",
"Servico": "Barba Completa"
}
]
}],
"2020-05-25T00:00:00": [{
"Kai": [
{
"nome": "Kai",
"apelido": "Martins",
"dt_extrato": "2020-05-26T00:00:00",
"dt_inicio": "2020-05-26T13:30:00",
"Servico": "Barba Completa"
},
{
"nome": "Kai",
"apelido": "Martins",
"dt_extrato": "2020-05-25T00:00:00",
"dt_inicio": "2020-05-25T08:30:00",
"Servico": "Corte Masculino"
}
],
"Estevan Alves": [
{
"nome": "Estevan Alves",
"apelido": "Estevan",
"dt_extrato": "2020-05-25T00:00:00",
"dt_inicio": "2020-05-25T08:40:00",
"Servico": "Corte Masculino"
},
{
"nome": "Estevan Alves",
"apelido": "Estevan",
"dt_extrato": "2020-05-25T00:00:00",
"dt_inicio": "2020-05-25T08:00:00",
"Servico": "Corte Masculino"
}
]
}]
}
Моя цель - сгруппировать услуги по дате и названию. Я мог сгруппировать только по одному или по другому, но не по обоим.
Чтобы сгруппировать по полю 'name', я использую следующий код:
this.name_group = this.array.reduce((data, object) => {
data[object.nome] = [...data[object.nome] || [], object];
return data;
}, {});
Для группировки по полю 'dt_extrato', я использую следующий код:
this.date_group = this.array.reduce((data, object) => {
data[object.dt_extrato] = [...data[object.dt_extrato] || [], object];
return data;
}, {});
Но мне не удается сгруппировать оба, вложенные. Возможно ли это, используя только сокращение?
Спасибо