I am currently working on the following code snippet:
import { HttpClient } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
@Component({
styleUrls: ["./styles.scss"],
templateUrl: "./template.html"
})
export class MyRouteData {
MyDataObject: object;
MyDataObjectTotals: object;
constructor(private http: HttpClient) {}
ngOnInit() {
this.http
.get("http://localhost:5000/MyRoute/GetMyData")
.subscribe(response => {
console.log(response);
this.MyDataObject = response;
});
}
}
The data stored in the MyDataObject object is as follows:
{
"Record1": {
"CustomerName": "Obi Wan Kenobi",
"TransactionDate": "2018-01-01",
"IsNewCustomer": false,
"ItemPuchased":"Speeder Polish 250ml",
"QuantityPurchased":2,
"SalesTotal":10.04
},
"Record2": {
"CustomerName": "Darth Vader",
"TransactionDate": "2018-01-02",
"IsNewCustomer": false,
"ItemPuchased":"Burn Cream 250ml",
"QuantityPurchased":200000,
"SalesTotal":7523840.84
},
...
}
Currently, I would like to calculate the totals of each column and store them in another object (defined as MyDataObjectTotals).
I have managed to sum up a single column using the following code:
var QuantityPurchasedColumn = MyDataObject.map(a => a.QuantityPurchased);
var QuantityPurchasedTotal = QuantityPurchasedColumn.reduce(function(a, b) {return a + b;});
Is there a more generic method that can take an object (such as MyDataObject) and return an object containing the total values for each column?
{
"CustomerName": null,
"TransactionDate": null,
"IsNewCustomer": null,
"ItemPuchased":null,
"QuantityPurchased": 200008,
"SalesTotal": 7675174.90
}