亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具有 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 關注
  • 89 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號