为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕娘9325324 2022-09-02 21:20:25
我想按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"    }]
查看完整描述

1 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

如果你想要获取嵌套对象,中间没有数组,则可以使用嵌套键数组来采用此方法。


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; }


查看完整回答
反对 回复 2022-09-02
  • 1 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信