英文:
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:
{\"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 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<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?
答案1
得分: 0
Your code seems correct, please recheck the output. Also, you can parse your object in the following way too.
英文:
Your code seems correct, please recheck the output. Also, you can parse your object in following way too.
答案2
得分: 0
你的示例在 TypeScript Playground 中正常运行并产生了预期的输出,可在此处查看(https://www.typescriptlang.org/play?#code/MYewdgzgLgBAlmOU4EMA2B5ARgKwKbCwC8MAUgMoYByAdAA4oBOEeAFAEQDeAOuwNZ4AngEZeALh7tgaAK7Q8jAPqM8AczjhxvYHjBRG6ObwC+AGl4DBAJnGTpcqAuVqNYLewDueeTMYg6eCbG7ACUAFCgkLDoaCqgjAAmEGIwAEoEIIkAPNCMCKqmMJww9vJKKurgKbn5MMYAfDAkCEiomLgEUADcYRHgECBoeDRoIKocMTBxmUkp7DAA1GSUtDVg6gBmgqwx04kQISE9fVEwMQAycNBNMNj4hDS6+nDeO2ixGfshNAC2KHSsVgAbUshQAboY8ABdEJNRqsThhGDIlHIhAJPAADxSoKRqJRFVcKQhsmGpUc5Rc4DxqOMh2OkQGQxGYzeaEu0COQA)
这表明可能有以下原因之一:
- 您的数据格式不正确
- 您正在修改输入的字符串形式,其
Object.entries
的key
将是索引0..length
- 您运行的代码与您发布的代码不匹配
在这种情况下,我会假设您运行的代码不匹配,因为您看到的是 [{"index":"0"},{"index":"1"},{"index":"2"} ... ]
,这表明 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 indices0..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 [{"index":"0"},{"index":"1"},{"index":"2"} ... ]
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论