解析和转换 TypeScript 对象会产生不期望的结果。

huangapple go评论60阅读模式
英文:

Parsing and converting a typescript object yields undesired result

问题

I got an object which looks like this:

{"key1":{"cluster_region":"centralus"},"key2":{"cluster_region":"westeurope"}}

I'm trying to make a list of objects from that given object. Each object in the list should have 2 fields called "index" and "region". The "index" value is the keys' values from the original object ("key1" and "key2") and the region value is the corresponding "cluster_region" from the original object.

I tried doing it like this:

const allrecords: Record<string, { cluster_region: string }> = initialObject;
console.log("all records: " + JSON.stringify(allrecords));
const allList = Object.entries(allrecords).map(([key, value]) => ({
index: key,
region: value.cluster_region
}));

I do see the correct value of allrecords in the console.log which is the initial object. However, the list itself I'm getting looks like this:

[{"index":"0"},{"index":"1"},{"index":"2"} ... ]

What am I doing wrong?

英文:

I got an object which looks like this:

{\&quot;key1\&quot;:{\&quot;cluster_region\&quot;:\&quot;centralus\&quot;},\&quot;key2\&quot;:{\&quot;cluster_region\&quot;:\&quot;westeurope\&quot;}}

I'm trying to make a list of objects from that given object. Each object in the list should have a 2 fields called "index" and "region". The "index" value is the keys' values from the original object ("key1" and "key2") and the region value is the corresponding "cluster_region" from the original object.

I tried doing it like this:

const allrecords: Record&lt;string, { cluster_region: string }&gt; = initialObject;
console.log(&quot;all records: &quot; + JSON.stringify(allrecords));
const allList = Object.entries(allrecords).map(([key, value]) =&gt; ({
        index: key,
        region: value.cluster_region
      }));

I do see the correct value of allrecords in the console.log which is the initial object. However, the list itself I'm getting looks like this:

[{&quot;index&quot;:&quot;0&quot;},{&quot;index&quot;:&quot;1&quot;},{&quot;index&quot;:&quot;2&quot;} ... ]

What am I doing wrong?

答案1

得分: 0

Your code seems correct, please recheck the output. Also, you can parse your object in the following way too.
解析和转换 TypeScript 对象会产生不期望的结果。

英文:

Your code seems correct, please recheck the output. Also, you can parse your object in following way too.
解析和转换 TypeScript 对象会产生不期望的结果。

答案2

得分: 0

你的示例在 TypeScript Playground 中正常运行并产生了预期的输出,可在此处查看(https://www.typescriptlang.org/play?#code/MYewdgzgLgBAlmOU4EMA2B5ARgKwKbCwC8MAUgMoYByAdAA4oBOEeAFAEQDeAOuwNZ4AngEZeALh7tgaAK7Q8jAPqM8AczjhxvYHjBRG6ObwC+AGl4DBAJnGTpcqAuVqNYLewDueeTMYg6eCbG7ACUAFCgkLDoaCqgjAAmEGIwAEoEIIkAPNCMCKqmMJww9vJKKurgKbn5MMYAfDAkCEiomLgEUADcYRHgECBoeDRoIKocMTBxmUkp7DAA1GSUtDVg6gBmgqwx04kQISE9fVEwMQAycNBNMNj4hDS6+nDeO2ixGfshNAC2KHSsVgAbUshQAboY8ABdEJNRqsThhGDIlHIhAJPAADxSoKRqJRFVcKQhsmGpUc5Rc4DxqOMh2OkQGQxGYzeaEu0COQA)

这表明可能有以下原因之一:

  • 您的数据格式不正确
  • 您正在修改输入的字符串形式,其 Object.entrieskey 将是索引 0..length
  • 您运行的代码与您发布的代码不匹配

在这种情况下,我会假设您运行的代码不匹配,因为您看到的是 [{&quot;index&quot;:&quot;0&quot;},{&quot;index&quot;:&quot;1&quot;},{&quot;index&quot;:&quot;2&quot;} ... ],这表明 index 被分配了数据的实际索引值。

请检查您在本地运行的代码,验证它与您所声明的数据是否匹配,然后相应地更新您的问题。

英文:

Your example of what you are doing runs properly and produces the expected output in the typescript playground, as visible here

This suggests that either:

  • Your data is malformed
  • You are modifying the string form of the input, who’s Object.entries key would be the indices 0..length
  • The code that you are running doesn't match the code that you posted.

I would assume in this case that the code you are running doesn't match, because you are seeing [{&quot;index&quot;:&quot;0&quot;},{&quot;index&quot;:&quot;1&quot;},{&quot;index&quot;:&quot;2&quot;} ... ] which suggests that index is being assigned the actual index value of the data.

Please check the code that you are running locally and verify that it, and the data that you are receiving match what you are stating, and then update your question accordingly.

huangapple
  • 本文由 发表于 2023年6月13日 17:40:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定