I am consuming a JSON to form a report, the JSON is like this:
[
{
"idscrap": 80,
"tipo": 1,
"fecha": "2022-09-01T15:24:04.353",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001031M8003",
"nombre": "BODY, FUEL TANK",
"numeroparte": "17500-3W0-A020-20",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 80,
"tipo": 1,
"fecha": "2022-09-01T15:24:04.353",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001031M8003",
"nombre": "TUBE COMP, FUEL VENT (ORVR)",
"numeroparte": "17725-17B-L710-Y1",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 80,
"tipo": 1,
"fecha": "2022-09-01T15:24:04.353",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001031M8003",
"nombre": "CLIP, ABSORBER",
"numeroparte": "91591-17B-3000",
"cantidad": 3,
"comment": ""
},
{
"idscrap": 80,
"tipo": 1,
"fecha": "2022-09-01T15:24:04.353",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001031M8003",
"nombre": "PIPE COMP, FUEL FILLER",
"numeroparte": "17660-3W0-A040-Y1",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 81,
"tipo": 1,
"fecha": "2022-09-01T15:24:32.963",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001001M8003",
"nombre": "BODY, FUEL TANK",
"numeroparte": "17500-3W0-A020-20",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 81,
"tipo": 1,
"fecha": "2022-09-01T15:24:32.963",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001001M8003",
"nombre": "TUBE COMP, FUEL VENT (ORVR)",
"numeroparte": "17725-17B-L710-Y1",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 81,
"tipo": 1,
"fecha": "2022-09-01T15:24:32.963",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001001M8003",
"nombre": "CLIP, ABSORBER",
"numeroparte": "91591-17B-3000",
"cantidad": 3,
"comment": ""
},
{
"idscrap": 81,
"tipo": 1,
"fecha": "2022-09-01T15:24:32.963",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001001M8003",
"nombre": "PIPE COMP, FUEL FILLER",
"numeroparte": "17660-3W0-A040-Y1",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 82,
"tipo": 1,
"fecha": "2022-09-01T15:25:21.173",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001041M8003",
"nombre": "BODY, FUEL TANK",
"numeroparte": "17500-3W0-A020-20",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 82,
"tipo": 1,
"fecha": "2022-09-01T15:25:21.173",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001041M8003",
"nombre": "TUBE COMP, FUEL VENT (ORVR)",
"numeroparte": "17725-17B-L710-Y1",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 82,
"tipo": 1,
"fecha": "2022-09-01T15:25:21.173",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001041M8003",
"nombre": "CLIP, ABSORBER",
"numeroparte": "91591-17B-3000",
"cantidad": 3,
"comment": ""
},
{
"idscrap": 82,
"tipo": 1,
"fecha": "2022-09-01T15:25:21.173",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001041M8003",
"nombre": "PIPE COMP, FUEL FILLER",
"numeroparte": "17660-3W0-A040-Y1",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 83,
"tipo": 1,
"fecha": "2022-09-01T15:25:48.97",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001021M8003",
"nombre": "BODY, FUEL TANK",
"numeroparte": "17500-3W0-A020-20",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 83,
"tipo": 1,
"fecha": "2022-09-01T15:25:48.97",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001021M8003",
"nombre": "TUBE COMP, FUEL VENT (ORVR)",
"numeroparte": "17725-17B-L710-Y1",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 83,
"tipo": 1,
"fecha": "2022-09-01T15:25:48.97",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001021M8003",
"nombre": "CLIP, ABSORBER",
"numeroparte": "91591-17B-3000",
"cantidad": 3,
"comment": ""
},
{
"idscrap": 83,
"tipo": 1,
"fecha": "2022-09-01T15:25:48.97",
"modelo": "3W0-A0",
"estacion": "Welding 5",
"correccion": "Breather desfasado",
"elemento": "2022083001021M8003",
"nombre": "PIPE COMP, FUEL FILLER",
"numeroparte": "17660-3W0-A040-Y1",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 84,
"tipo": 2,
"fecha": "2022-09-01T15:27:37.513",
"modelo": "3W0-A0",
"estacion": "Corte y persing ",
"correccion": " ",
"elemento": "2022083001071M8003",
"nombre": "BODY, FUEL TANK",
"numeroparte": "17500-3W0-A020-20",
"cantidad": 1,
"comment": ""
},
{
"idscrap": 85,
"tipo": 2,
"fecha": "2022-09-01T15:28:18.597",
"modelo": "3W0-A0",
"estacion": "Corte y persing ",
"correccion": " ",
"elemento": "2022083000891M8003",
"nombre": "BODY, FUEL TANK",
"numeroparte": "17500-3W0-A020-20",
"cantidad": 1,
"comment": ""
}
]
Within the report I must deliver a table where one of its columns is the numeroparte
and another column is the total, that is, if numeroparte
it is repeated n
number of times, I must add its value cantidad
, I must expect something like this:
I have a function where I treat the JSON as an array, I get the first element in its property numeroparte
and compare it against the rest, so far I get the sum of the first element, the next step I see would be, check if I already counted that element, for that I use the array part_market
, where I enter the elements inside the cycle, later it helps me to check whether or not I should add data_report
to the sum that I did, but it does not add any data.
function build_totals(array)//Construye totales para cada parte encontrada en el reportel
{
var part_market = [];//Para almacenar los elementos que he validado
for (var d = 0; d < array.length; d++)
{
var count = 0;
part_market.push(array[d].numeroparte);
for (var e = 0; e < array.length; e++)
{
if (array[e].numeroparte == array[d].numeroparte)
{
count = count + array[e].cantidad;
}
}
if (!part_market.includes(array[d].numeroparte))//Si el elemento no se encuentra agregarlo al array
{ data_report.push({ NumParte: array[d].numeroparte, Cantidad: count }); }
}
console.log(data_report);
}
I show you a solution in which I create an object in which I add properties "part number" and as value I assign the quantity and if the key already exists I add the quantity.
I think that is what you are looking for and if not, then it will help you
If you have to do a lot of these tables, you might want to check out this alasql library . With which you can make an SQL query to a javascript array