I have the following array grouped by the name field of my array, what I want is to reduce the elements of each group by the c_prod field, that is, if I have the same c_prod twice in the group, reduce it to 1 by adding the s_cant field which is the quantity, keeping the s_vent which is the sale price
const categories = [
{ s_cant: 1, s_vent: 15, name: 'juan carlos', prod: 'carne', c_prod: '02', province: '01' },
{ s_cant: 1, s_vent: 10, name: 'fredy alarcon olivera', prod: 'pollo', c_prod: '01', province: '02' },
{ s_cant: 1, s_vent: 10, name: 'fredy alarcon olivera', prod: 'pollo', c_prod: '01', province: '03' },
{ s_cant: 1, s_vent: 15, name: 'lucas lopez', prod: 'carne', c_prod: '02', province: '01' },
{ s_cant: 2, s_vent: 15, name: 'juan carlos', prod: 'carne', c_prod: '02', province: '01' },
{ s_cant: 2, s_vent: 10, name: 'lucas lopez', prod: 'pollo', c_prod: '01', province: '02' },
{ s_cant: 2, s_vent: 10, name: 'juan carlos', prod: 'pollo', c_prod: '01', province: '03' },
{ s_cant: 3, s_vent: 10, name: 'juan carlos', prod: 'pollo', c_prod: '01', province: '02' },
{ s_cant: 4, s_vent: 15, name: 'fredy alarcon olivera', prod: 'carne', c_prod: '02', province: '01' },
];
const agrupado = categories.reduce((p,c)=>{
const group = c.name.replace(/ /g,"_");
if(group in p){
p[group].push(c);
}
else{
p[group] = [c];
}
return p;
},{});
const resultado = Object.keys(agrupado).map(g=>{
return [{l_client:g.replace(/_/g," ")}].concat(agrupado[g]);
});
var array = resultado;
for ( var element = 0; element < array.length; element++ ) {
var acumulador = 0;
for ( var element_suma = 1; element_suma < array[element].length; element_suma++ ) {
acumulador += ( parseFloat(array[element][element_suma].s_vent)) * ( parseFloat(array[element][element_suma].s_cant));
}
array[element][0]['total'] = acumulador;
}
console.log(array);
the result you would like to get
[
[
{
"l_client": "juan carlos",
"total": 80
},
{
"s_cant": 3,
"s_vent": 10,
"name": "juan carlos",
"prod": "carne",
"c_prod": "02"
},
{
"s_cant": 5,
"s_vent": 10,
"name": "juan carlos",
"prod": "pollo",
"c_prod": "01"
}
],
[
{
"l_client": "fredy alarcon olivera",
"total": 60
},
{
"s_cant": 2,
"s_vent": 10,
"name": "fredy alarcon olivera",
"prod": "pollo",
"c_prod": "01"
},
{
"s_cant": 4,
"s_vent": 10,
"name": "fredy alarcon olivera",
"prod": "carne",
"c_prod": "02"
}
],
[
{
"l_client": "lucas lopez",
"total": 30
},
{
"s_cant": 1,
"s_vent": 10,
"name": "lucas lopez",
"prod": "carne",
"c_prod": "02"
},
{
"s_cant": 2,
"s_vent": 10,
"name": "lucas lopez",
"prod": "pollo",
"c_prod": "01"
}
]
]
I propose a structure that seems more logical, where each element of the array is an object with 3 properties: Customer name, sale amount and an array with product details:
You can do it with a single
.reduce()
, without the need to group first.This simplifies the logic, both for creating the array and accessing it later. Although, if you want it with the format exposed in your question, it can also be done; check the following snippet: