Calculate total from an array of object contains similar items.

Function to find the total from an array of object if it contains similar items.

var groupedByItem = arr.reduce(function (result, current) {
   var item = current.Item;
   if (!result[item]) {
      result[item] = {
         Type: current.Type,
         Item: item,
         TotalQuantity: 0
      };
   }
   result[item].TotalQuantity += parseInt(current.Quantity);
   return result;
}, {});
var resultArray = Object.values(groupedByItem);

Leave a comment

Your email address will not be published. Required fields are marked *