1 回答
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; }
添加回答
举报