Combining multiple arrays of numbers in Typescript into one aggregate

I am looking to combine multiple number values from different arrays in Typescript. My data model looks like this:

export class DataModel {

    date : string;
    number : any;

}

The main class contains an array of DataModels:

export class CountryData {

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

}

Furthermore, I have an array of CountryData.

For example, two arrays of CountryData each containing three DataModel values:

    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 want to loop through the data variable and get a result like this in the countrySum.dataset:

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

This should be applicable to n arrays in data. Does anyone know how to achieve this using functions such as map, reduce, or others?

Thanks!

Answer №1

I wanted to express my gratitude as well. I followed the traditional approach: taking it step by step...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a dynamic dropdown menu to display nested arrays in Vuejs

I have some important data https://i.sstatic.net/Cq2t6.png Challenge I'm facing difficulty in accessing the tubes array within my data Solution script data() { return { types: [] } }, m ...

Challenges arising from the implementation of dependencies in gateways and other modules

Having been using nestjs for a while, I have found it to be incredibly useful, especially as projects become larger and more complex. However, I am currently facing an issue with dependency injection. I have always used it between modules of the same type ...

What is the best way to update the value of a variable within a specific child component that is displayed using ngFor?

Hello there, I'm in need of some assistance with a coding issue. I have a parent component named "users-list" that displays a list of child components called "user" using *ngFor. Each component's class is dynamic and depends on various internal v ...

Vue 3 - Compelled to utilize any data type with computedRef

Recently, I've been diving into Vue/Typescript and encountered a puzzling error. The issue revolves around a class named UploadableFile: export class UploadableFile { file: File; dimensions: Ref; price: ComputedRef<number>; ... constr ...

There seems to be an issue with the Typescript version compatibility in the node_modules folder for the Angular Material table cell component

While working on my project with Angular, I encountered an issue. I attempted to downgrade my TypeScript version to 3.9.7 but the problem persists. Below are the dependencies listed in my package.json: "private": true, "dependencies&qu ...

Implementing indexers in TypeScript to accommodate both string and numeric keys

Seeking to incorporate different types into existing code. In the code, there exists a transitionData object in which objects can be added by index as shown below: this.transitionData[id] = transition where id is a number and transition is of type Trans ...

Deleting specific URLs in Markdown using TypeScript

Currently, I am working with Typescript and Node v8, aiming to process markdown text and eliminate all image URLs that match specific criteria: URLs containing the term "forbidden" URLs with IP addresses I have been using the following regular expression ...

Confusing generic function overload in TypeScript

During my exploration of TypeScript, I came across an unusual situation. function concat5<T>(strs: T, strs2: T): T; function concat5(strs: string, strs2: string) { return strs + strs2; } concat5(123, 12); concat5({a:1}, {b:2}); Upon reviewing ...

Transforming Bootstrap using Typescript styled components

I have been working on customizing the Navbar in my React app using Typescript's styled components. However, I am facing difficulties in restyling the default styles from Bootstrap that I have copied. Here is an example of the React Bootstrap Navbar c ...

Add an element to a nested array using the array_push function

Below is the code snippet I am working with: $return_array = array( $count_answers => array( "name" => $domain, "type" => $type, "class" => $class, "tt ...

Determine the shared elements within a single array by utilizing Javascript

I have the following array of mergeUniqueItems: var mergeUniqueItems = ["-JsDEcxz_ZSGFLKwd1QM", "-JsJ2NXGDYKI6QRsuXVK", "-JsJ2RK-kOG2eGcG04xF", "-JsJ2RK-kOG2eGcG04xF", "-JsJ2YLPiP6751zh8geS"] After using this code snippet, I encountered an issue: ...

Why isn't my API's JSON Response showing up in PHP?

Having trouble displaying the JSON response from an API call in PHP. Despite confirming that the server, IP settings, and API status are all fine on the provider's end, I am unable to identify the problem. This is how I'm handling the PHP part: ...

Modify the property of the ChildComponent by utilizing the ViewChild method

I recently started exploring Angular and I've been experimenting with ViewChild and ViewChildren. In one scenario, I have a property called today = new Date() in my Component2. I'm accessing this property in Component1 using ViewChild and continu ...

Is there a way to set up custom rules in eslint and prettier to specifically exclude the usage of 'of =>' and 'returns =>' in the decorators of a resolver? Let's find out how to implement this

Overview I am currently working with NestJS and @nestjs/graphql, using default eslint and prettier settings. However, I encountered some issues when creating a graphql resolver. Challenge Prettier is showing the following error: Replace returns with (r ...

Detecting changes in Angular when the @Input() value remains the same

I have created an Angular Custom scroll directive that utilizes an @Input() to pass an HTML element as a parameter, allowing the scrollbar to move to that specific element. However, I've encountered an issue where if I pass the same HTML Element mult ...

Is there a way to listen for router config Data subscription upon NavigationEnd event?

At the moment: I'm using router.events to fetch information from {path: '', component: HomeComponent, data:{header:true}}, router configuration ngOnInit() { this.router.events.filter(e => e instanceof NavigationEnd).subscribe(event ...

Ways to verify the order of the information within an array extracted from a file using JavaScript

After extracting marker data from a file containing USFM data at the provided USFM_file_link, I created an array featuring markers from each line: ['id','c','p','v','p','v','v','v& ...

Compilation error occurred when running Angular with mat-form: ngcc encountered an issue while processing [email protected]

Currently dealing with a compile error in a small mat-form example that I created. Unfortunately, I am unable to pinpoint the exact issue causing this error. If you have a moment, please take a look at the code here: https://stackblitz.com/edit/angular-iv ...

Currently in the process of creating a carousel displaying images, I have encountered an issue stating: "An optional property access cannot be on the left-hand side of an assignment expression."

I am currently working on an Angular Image Carousel that utilizes a model to iterate through the images. However, I am encountering an error when attempting to access the first position. An error message stating "The left-hand side of an assignment expres ...

In Angular 17, is there a way to trigger a component's method when a Signal is modified?

Our component is designed to monitor signals from a Service: export class PaginationComponent { private readonly pageSize = this.listService.pageSize.asReadonly(); private readonly totalCount = this.listService.totalCount.asReadonly(); readonly pag ...