TypeScript: Unable to retrieve values or keys from a Map data structure

Issue arises when attempting to access map keys or values using a method, resulting in the following error:

ERROR TypeError: factors.values is not a function or its return value is not iterable

These model interfaces are used for object types:

export interface Plant{
    name: string;
    templateName: string;
}

export interface RiskFactor {
    riskId: number;
    name: string;
    slotName: string;
    factorType: string;
    plantType: string;
}

export interface Symptom {
    symptomId: number;
    name: string;
    slotName: string;
    plantType: string;
    checked: boolean;
}


export interface DiagnoseForm {

riskFactors: Map<string, RiskFactor[]>;
symptoms: Symptom[];

}

The server response is saved in the "formData" variable with DiagnoseForm, allowing normal access and creation of formArray from symptoms:

  buildSymptoms(){
  const arr = this.formData.symptoms.map(symptom => {
    symptom.checked = false;
    return this.formBuilder.control(false);
  });
  return this.formBuilder.array(arr);
  }

Error occurs when trying to retrieve the riskFactors arrays from the map leading to the above-mentioned issue in the following code:

  buildRiskFactors(){


const factors: Map<string, RiskFactor[]> = this.formData.riskFactors;

for( let wrapper of factors.values()){
  console.log(wrapper);
}

}

Various attempts have been made to resolve this problem but all efforts result in the same output.

Answer №1

It is recommended to use Object.values(factors) rather than factors.values()

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

Leverage React components for efficient code reuse and export modules for

I have Project X, which was built using the command "yarn build" and it generated a main.js file. I am trying to use this main.js file as a dependency for another React project Y. Unfortunately, following the steps from React components and module exports ...

Utilizing express-session in TypeScript: Incorporating user information into the session

Hey, I'm currently working on implementing express-session and connect-mongodb-session in my TypeScript Express/Node API. My goal is to use express-session to create a cookie for logged-in users and automatically persist that cookie to MongoDB. Howeve ...

Resetting forms in Angular 5: What you need to know

When a user submits a current value, I am working quickly to provide them with new value. However, after resetting the form and serving the new value, there seems to be an issue where the form reset execution is asynchronous, resulting in the user receivin ...

learning how to transfer a value between two different components in React

I have 2 components. First: component.ts @Component({ selector: "ns-app", templateUrl: "app.component.html", }) export class AppComponent implements OnInit { myid: any; myappurl: any; constructor(private router: Router, private auth: ...

Is it possible to globally define a namespace in Typescript?

Seeking a way to make my Input module accessible globally without the need for explicit path definitions. Currently, I have to import it like this: import { Input } from "./Input/Input";. Is there a method to simplify the import statement for modules con ...

Firebase Functions Project encountering a "Cannot find module" error in VS Code

While working on a firebase functions project in Visual Studio Code, I encountered an issue inside the index.ts file. The imported modules were not being recognized even though autocomplete showed that the modules exist. When attempting to import them, I k ...

Limit the allowable React Component prop type in Typescript

To promote composition within our codebase, we utilize passing components as props. Is there a way to enforce type checking for the components passed as props? For instance, let's consider a commonly used Typography component throughout the codebase, ...

Sending array values from a dynamic input field in Angular 4 and processing them accordingly

I am currently exploring options on how to add and delete multiple input fields. When a user submits two or more fields, I want the results to be displayed in an array. This is my HTML form: <form method="post" [formGroup]="formData"> ...

Trouble importing class using path alias in Typescript encountered

I am currently experimenting with Typescript and OvernightJS and encountering an issue while trying to import a class into my controller. I received an error message that says: Error: Cannot find module '@Models/company' Interestingly, when I ...

Googlebot is unable to view structured data tags generated in an Angular single-page application (SPA)

During live testing a URL on Google Search Console, I discovered that while Googlebot can render the page, the structured data is missing. The structured data is generated in JavaScript using the following function: insertStructuredData(genDataFn: Function ...

What is the reason for TypeScript's decision to lazily evaluate constrained class generics?

I am experiencing confusion with the TypeScript declaration provided below. class C<T extends {}> { method() { type X = T extends {} ? true : false; // ^? type X = T extends {} ? true : false; // Why is X not `true`? ...

Is it better to wait for an ajax request to finish loading in a vue or angular SSR environment?

Currently, I have a Vue or Angular application in place. Upon loading the app component, an AJAX request is made to retrieve the data that will be displayed in the child components. In addition, I am utilizing server-side rendering (SSR). My query pertai ...

Explore the titles provided by Wikipedia

Hi there, I'm fairly new to Angular and trying to work with the Wikipedia API. However, I seem to be having trouble getting 4 titles from the API. Here's an example URL for one of the titles: https://en.wikipedia.org/w/api.php?action=query&pr ...

Utilizing the Docker COPY command to exclusively transfer files of a specific file type extension

I have been working on implementing a new build process for one of our Angular applications. In order to reduce the bundle size, I have included a build step that involves gzipping all the files in the dist folder after the build process. After the build, ...

Issues with TypeScript: Difficulty locating names in HTML templates

I recently upgraded my Angular 7 code to Angular 9 by following the steps outlined in the Angular Upgrade guide. However, upon completion of the migration process, I started encountering numerous "Cannot find name" errors within the HTML templates of my co ...

Applying background color within an *ngFor loop

My question may not be perfectly described by the title, but here it is. In my Angular Material2 project, I have an md-toolbar where I am dynamically looping through values: <md-toolbar (click)="selectedToolbarValue(key.Name)" *ngFor="let key of array ...

Encountering difficulties in generating a personalized Angular Element

Currently, I am in the process of developing a custom Component that needs to be registered to a module. Here is how it is being done: app.module.ts import { createCustomElement } from "@angular/elements"; @NgModule({ declarations: [ExtensionCompone ...

Having trouble getting a Jasmine test to pass for a basic Angular2 component

Here is a snippet of my basic component setup: @Component({ selector: 'messages', styleUrls: ['messages.component.scss'], templateUrl: 'messages.component.html', }) export class MessagesComponent implements OnInit ...

Having trouble with CSS Locator identifying an element in your Protractor Test?

In the Protractor test snippet below, I am attempting to calculate the quantity of div elements using the CSS locator: let list = element.all(by.css('div[class="ag-header-cell ag-header-cell-sortable"]')); expect(list.count()).toBe(11); ...

Implicated Generic in TypeScript

Is there a way to simplify my class A implementation? export class A<TB extends B<TC>, TC> implements TD<TB, TC> { make(): TC {} } Currently, I have to specify the TC type every time I create an instance of A: class CTest {} class BTes ...