简体   繁体   中英

Map.prototype.forEach does not exist Angular 6 project

I am attempting to take a C# Dictionary object from a Http response and cast it to a Map in JavaScript. I am unable to figure out why the heck I cannot use Map.prototype.forEach. It keeps telling me that it does not exist. According to the MDN for Map.prototype.forEach it was first introduced in ES2015. I included ES2015, ES2015.collection, and ES2015.iterable in the lib section of the tsconfig. I figured that would be enough but it does not seem to be working at all. Is there something that I am missing?

Below is my tsconfig file:

{
  "compileOnSave": false,
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "lib": [
      "es5",
      "es2015",
      "es2015.collection",
      "es2015.iterable",
      "es2016",
      "es2017",
      "dom"
    ],
    "allowJs": false,
    "checkJs": false,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "outDir": "./dist/out-tsc",
    "alwaysStrict": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "typeRoots": [
      "node_modules/@types"
    ],
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
  }
}

Code I am attempting to use:

myMap: Map<number, Array<number>> = new Map<number, Array<number>>();
this.myMap.forEach((value: Array<number>) => {  /* ERROR HERE */
  console.log(value);
})

Here is the associated file for the Map interface Here is the associated code Map interface

Here is a little info about the project:

Angular v6.0.4

Angular CLI v6.0.8

Typescript v2.7.2

The problem stemmed from trying to take a C# Dictionary object in a Http response and type it as a Map. This just does not natively work. The response was returning a generic JavaScript Object of { K: number, V: number[] } even though I was typing it as a Map. Obviously an Object does not have the function forEach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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