简体   繁体   English

Typescript 中两个或多个数字数组的聚合

[英]Aggregate of two or more arrays of numbers in Typescript

I'd like to aggregate two or more number values from several arrays in Typescript.我想从 Typescript 中的几个数组中聚合两个或多个数值。 I have a data model as follows:我有一个数据模型如下:

export class DataModel {

    date : string;
    number : any;

}

The main class has an array of DataModels itself:主类本身有一个 DataModel 数组:

export class CountryData {

    country: string;
    province: string;
    Long: any;
    Lat: any;
    dataset: DataModel[] = [];

}

Finally, I have an array of CountryData.最后,我有一个 CountryData 数组。

Exemple of two CountryData arrays with three DataModel values each one:两个 CountryData 数组的示例,每个数组具有三个 DataModel 值:

    let data: CountryData[];
    let country1: new CountryData();
    let country2: new CountryData();
    let countrySum: new CountryData();

    country1.dataset = [{'01/02/20',5}, {'01/03/20',10}, {'01/04/20',15}];
    country2.dataset = [{'01/02/20',5}, {'01/03/20',10}, {'01/04/20',15}];

    data.push(country1);
    data.push(country2);

I'd like to iterate the data variable and obtain a result like this into the countrySum.dataset:我想迭代数据变量并在 countrySum.dataset 中获得这样的结果:

[{'01/02/20',10}, {'01/03/20',20}, {'01/04/20',30}];

And this, will apply to n arrays in data.这将适用于数据中的 n 个数组。 Anyone knows how to do this with functions like map, reduce or any other?任何人都知道如何使用 map、reduce 或任何其他函数来做到这一点?

Thansk!谢谢!

顺便说一句,谢谢......我已经按照老派的方法做了:为了......为了......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM