In my input array of object, I have a field called Grade. This is the filtering parameter
var input = [
{
"Qty": "2.00",
"Grade": "AU27",
"Thickness": "5.00",
"Width": "1200.00",
},
{
"Qty": "7.00",
"Grade": "AU27",
"Thickness": "10.00",
"Width": "1400.00",
},
{
"Qty": "17.00",
"Grade": "AU27",
"Thickness": "5.00",
"Width": "1700.00",
},
{
"Qty": "51.00",
"Grade": "FE500D",
"Thickness": "10.00",
"Width": "1100.00",
},
{
"Qty": "69.00",
"Grade": "FE500D",
"Thickness": "12.00",
"Width": "1500.00",
},
{
"Qty": "30.00",
"Grade": "FE500D",
"Thickness": "8.00",
"Width": "1800.00",
},
{
"Qty": "92.00",
"Grade": "FE500D",
"Thickness": "10.00",
"Width": "2200.00",
},
{
"Qty": "98.00",
"Grade": "FE600D",
"Thickness": "11.00",
"Width": "2400.00",
},
{
"Qty": "115.00",
"Grade": "FE600D",
"Thickness": "17.00",
"Width": "2600.00",
}
];
I want to create array of object called sumGradeArray from the above input array. If we consider the total quantities of different grades, irrespective of thickness and width, then total quantity of Grade AU27 is 2.00 + 7.00 + 17.00 = 26.00.
Similarly for grade FE500D total quantity is 51.00 + 69.00 + 30.00 + 92.00 = 242.00
Similarly for grade FE600D total quantity is 98.00 + 115.00 = 213.00
var sumGradeArray = [
{
"Grade": "AU27",
"TotalQty": "26.00",
},
{
"Grade": "FE500D",
"TotalQty": "242.00",
},
{
"Grade": "FE600D",
"TotalQty": "213.00",
},
];
Please someone provide me a generic solution using Vanilla JS(no Jquery/Lodash). I am showing just 3 grades here, in reality the input array of object is an API response. It can have hundreds of grades