简体   繁体   English

Angular TypeScript Map 键值对不保存

[英]Angular TypeScript Map with key value pairs not saving

Within an Angular web application I have a method that is called when the user presses a button.在 Angular web 应用程序中,我有一个在用户按下按钮时调用的方法。 Based on that user action I gather some values from labels and text input fields and want to store them in key, value pairs in a Map object.基于该用户操作,我从标签和文本输入字段中收集了一些值,并希望将它们存储在 Map object 中的键值对中。 That Map object in turn should become part of another object and sent to the back-end/RESt API. Map object 应该依次成为另一个 object 的一部分并发送到后端/REST ZDB974238714CA8DE634A7ACE1

However, it seems that the map is never actually saved.但是,map 似乎从未真正保存过。 I am kind of baffled and confused on how this is happening.我对这是如何发生的感到困惑和困惑。

It's the code below (example one of filling the map):这是下面的代码(填充地图的示例之一):

let testMap = new Map([['testers','tester'],['235235','2124124'],['2352352','2124124']]);
console.log('Map Size: ' + testMap.size);
console.log('Map Output: ' + JSON.stringify(testMap));

Output being:
Map Size: 3
Map Output: {}

And the code below (example two of filling the map):以及下面的代码(填充地图的示例二):

let testMap:Map<string,string> = new Map<string,string>();
testMap.set('tester1','1234');
testMap.set('tester2','4567');

console.log('Map Size: ' + testMap.size);
console.log('Map Output: ' + JSON.stringify(testMap));


Output being:
Map Size: 2
Map Output: {}

Here my tsconfig.json file:这是我的 tsconfig.json 文件:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    //"target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

In both cases it looks like the Map is empty, while it's size is shown correctly.在这两种情况下,Map 看起来都是空的,但它的大小显示正确。 On the back-end side it seems I am also getting an empty Map.在后端,我似乎也得到了一个空的 Map。 Am I missing something here?我在这里错过了什么吗? Or doing something wrong here?或者在这里做错了什么?

Your testMap has values.您的testMap有值。 you can see it by你可以通过

  JSON.stringify(Object.fromEntries(testMap))

or或者

  JSON.stringify([...testMap]);

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

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