2个属性上的GROUP-BY数组和REDUTE

2022-05-24 00:00:00 arrays group-by reduce javascript

我想按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_Extra’分组,我使用了以下代码:

 this.date_group = this.array.reduce((data, object) => {
      data[object.dt_extrato] = [...data[object.dt_extrato] || [], object];
      return data;
    }, {});

但我不能同时对嵌套的两个进行分组。是否可以仅使用REDUTE?

谢谢


解决方案

如果您希望获得中间没有数组的嵌套对象,则可以使用嵌套键数组来采用此方法。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
var data = [{ 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" }],
    keys = ['dt_extrato', 'nome'],
    result = data.reduce((r, object) => {
        keys
            .reduce((group, key, index, { length }) =>
                group[object[key]] = group[object[key]] || (index + 1 === length
                    ? []
                    : {}
                ), r)
            .push(object);
        return r;
    }, {});
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

相关文章