具有 reduce 的 2 个属性上的分组依据数组

我想按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"

    }

]




慕娘9325324
浏览 74回答 1
1回答

DIEA

如果你想要获取嵌套对象,中间没有数组,则可以使用嵌套键数组来采用此方法。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; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript