按 "dataPrin" 字段对对象数组进行分组
Group array of objects by "dataPrin" field
我尝试了一段时间按“dataPrin”字段对这个数组进行分组,但没有成功。
请注意,我有两个“dataPrin”字段显示相同的日期,对吧!
我想对这个数组进行分组,这样如果有两个“dataPrin”字段显示相同的日期,它们将被分组为一个,如下例所示。
是否可以为此使用 'reduce'?我想得到社区的帮助。
当前数组:
[{
"dataPrin": "2021-05-31T16:10:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1510,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:30:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1511,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:15:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1512,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
}]
所需数组
[{
"dataPrin": "2021-05-31T16:10:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1510,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:30:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1511,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
},
{
"cod_transacao": 1512,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
}]
这非常适合 reduce()
。
const data = [{
"dataPrin": "2021-05-31T16:10:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1510,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:30:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1511,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:15:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1512,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
}
]
let ndata = data.reduce((b, a) => {
let iden = a.dataPrin.split("T")[0];
if (b.hasOwnProperty(iden)) a.lancamentos.forEach(l => b[iden].lancamentos.push(l));
else b[iden] = a;
return b
}, {})
console.log(ndata);
你在这里:
var data = [{
"dataPrin": "2021-05-31T16:10:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1510,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:30:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1511,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:15:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1512,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
}]
const temp = data.reduce((res,item)=>{
const date = item.dataPrin.split('T')[0];
if(date in res){
res[date].lancamentos.push(...item.lancamentos)
}else{
res[date] = item;
}
return res
},{});
const desired = Object.keys(temp).map(c=>temp[c])
我尝试了一段时间按“dataPrin”字段对这个数组进行分组,但没有成功。
请注意,我有两个“dataPrin”字段显示相同的日期,对吧!
我想对这个数组进行分组,这样如果有两个“dataPrin”字段显示相同的日期,它们将被分组为一个,如下例所示。
是否可以为此使用 'reduce'?我想得到社区的帮助。
当前数组:
[{
"dataPrin": "2021-05-31T16:10:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1510,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:30:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1511,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:15:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1512,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
}]
所需数组
[{
"dataPrin": "2021-05-31T16:10:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1510,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:30:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1511,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
},
{
"cod_transacao": 1512,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
}]
这非常适合 reduce()
。
const data = [{
"dataPrin": "2021-05-31T16:10:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1510,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:30:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1511,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:15:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1512,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
}
]
let ndata = data.reduce((b, a) => {
let iden = a.dataPrin.split("T")[0];
if (b.hasOwnProperty(iden)) a.lancamentos.forEach(l => b[iden].lancamentos.push(l));
else b[iden] = a;
return b
}, {})
console.log(ndata);
你在这里:
var data = [{
"dataPrin": "2021-05-31T16:10:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1510,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:30:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1511,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
},
{
"dataPrin": "2021-05-29T16:15:28-03:00",
"selectAll": false,
"lancamentos": [{
"cod_transacao": 1512,
"num_cpf_cnpj": "11.414.555/0001-04",
"dt_lancamamento": "2021-05-31T16:29:28-03:00",
}]
}]
const temp = data.reduce((res,item)=>{
const date = item.dataPrin.split('T')[0];
if(date in res){
res[date].lancamentos.push(...item.lancamentos)
}else{
res[date] = item;
}
return res
},{});
const desired = Object.keys(temp).map(c=>temp[c])