如何在打字稿中对多维对象中的数据求和?

时间:2018-09-26 00:15:05

标签: angular typescript

我有以下代码:

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;
      });
  }
}

将以下数据存储在MyDataObject对象中:

{
  "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
  },
  "Record3": {
    "CustomerName": "Luke Skywalker",
    "TransactionDate": "2018-01-02",
    "IsNewCustomer": false,
    "ItemPuchased":"Power Converters",
    "QuantityPurchased":5,
    "SalesTotal":1324.02
  },
  "Record4": {
    "CustomerName": "Jabba the Hut",
    "TransactionDate": "2019-01-05",
    "IsNewCustomer": false,
    "ItemPuchased":"Dancing Slave Chick",
    "QuantityPurchased":1,
    "SalesTotal":150000.00
  }
}

请注意,GetMyData端点正在运行返回表的SQL查询。我刚刚在这里将数据表示为JSON,以便可以轻松查看。 SQL查询返回的列为CustomerName,TransactionDate,IsNewCustomer,ItemPuchased,PurchasePurchased和SalesTotal。

我想将每列的总计(在相关时)存储到另一个对象中(上面的代码中的MyDataObjectTotals)。

我可以用以下代码总计一列:

var QuantityPurchasedColumn = MyDataObject.map(a => a.QuantityPurchased);
var QuantityPurchasedTotal = QuantityPurchasedColumn.reduce(function(a, b) {return a + b;});

有没有我可以编写的通用方法来给它一个对象(即MyDataObject),它将为我返回一个包含以下数据的对象(同样,不是JSON字符串,而是一个具有CustomerName,TransactionDate,IsNewCustomer, ItemPuchased,QuantityPurchased和SalesTotal列):

{
    "CustomerName": null,
    "TransactionDate": null,
    "IsNewCustomer": null,
    "ItemPuchased":null,
    "QuantityPurchased": 200008,
    "SalesTotal": 7675174.90
}

3 个答案:

答案 0 :(得分:1)

您尝试过JSON.Parse(myObject)方法吗?这样会将您的JSON对象转换为JS对象。

答案 1 :(得分:1)

我可以想到两种不同的解决方法。 1。遍历json键。

git submodule update --recursive --remote --merge --force

2。而不是发送记录的json,而是发送记录数组

var myObj = {

  "Record1": {
    "CustomerName": "Obi Wan Kenobi",
    "TransactionDate": "2018-01-01",
    "IsNewCustomer": false,
    "ItemPuchased":"Speeder Polish 250ml",
    "QuantityPurchased":2,
    "SalesTotal":10.04
  },
    "Record2": {
    "CustomerName": "Oobi",
    "TransactionDate": "2018-01-01",
    "IsNewCustomer": false,
    "ItemPuchased":"Speeder Polish 250ml",
    "QuantityPurchased":2,
    "SalesTotal":10.04
  },
};

for (var values in myObj) {
  console.log(myObj[values]['QuantityPurchased']);
}

答案 2 :(得分:1)

基于您成功使用map的声明,我假设您打算将MyDataObject用作数组而不是问题中所示的对象。您可以使用for-in循环遍历第一条记录的键,并为每个键检查是要和还是null

var MyDataObject = [
  {
    "CustomerName": "Obi Wan Kenobi",
    "TransactionDate": "2018-01-01",
    "IsNewCustomer": false,
    "ItemPuchased": "Speeder Polish 250ml",
    "QuantityPurchased": 2,
    "SalesTotal": 10.04
  },
  {
    "CustomerName": "Darth Vader",
    "TransactionDate": "2018-01-02",
    "IsNewCustomer": false,
    "ItemPuchased": "Burn Cream 250ml",
    "QuantityPurchased": 200000,
    "SalesTotal": 7523840.84
  },
  {
    "CustomerName": "Luke Skywalker",
    "TransactionDate": "2018-01-02",
    "IsNewCustomer": false,
    "ItemPuchased": "Power Converters",
    "QuantityPurchased": 5,
    "SalesTotal": 1324.02
  },
  {
    "CustomerName": "Jabba the Hut",
    "TransactionDate": "2019-01-05",
    "IsNewCustomer": false,
    "ItemPuchased": "Dancing Slave Chick",
    "QuantityPurchased": 1,
    "SalesTotal": 150000.00
  }
];
type MyRecord = typeof MyDataObject[number];

var SumColumns: Partial<{ [K in keyof MyRecord]:
  MyRecord[K] extends number ? true : never }> = {
  "QuantityPurchased": true,
  "SalesTotal": true
};

var Sum = <{ [K in keyof MyRecord]: number | null }>{};
let columnName: keyof MyRecord;
for (columnName in MyDataObject[0]) { 
  Sum[columnName] = SumColumns[columnName]
    ? MyDataObject.map(a => <number>a[columnName]).reduce(function (a, b) { return a + b; })
    : null;
}