简体   繁体   English

如何进行JSON.parse并忽略数组索引?

[英]How to JSON.parse and ignore the array indices?

I have this sample string which I want to parse as an Object 我有此示例字符串,我想将其解析为对象

const payload = "{\"teamIds\":[584,585,586]}";

console.log(JSON.parse(payload))

However the result automatically adds the indices: 但是,结果会自动添加索引:

teamIds:
  0: 584,
  1: 585,
  2: 586

How do I get rid of the indices in this case and just keep [584,585,586] as is? 在这种情况下,如何摆脱索引并保持[584,585,586]为原样?

EDIT 编辑

Coming from a PHP background, I thought this one was possible. 来自PHP背景,我认为这是可能的。 Nevertheless, thank you for all your help. 不过,感谢您的所有帮助。

You cannot, that's how arrays work, they need to have indexes for values that are automatically set if you don't provide any of yours. 您不能,因为数组是这样工作的,如果您不提供任何值,则它们需要具有自动设置的值的索引。 That's just how it works, in javascript, you can assign index, but it then becomes an object, not an array, but there cannot be a value in an array without index. 这就是它的工作方式,在javascript中,您可以分配索引,但是它随后成为一个对象,而不是数组,但是没有索引的数组中就不能有值。

const payload = "{\"teamIds\":[584,585,586]}";

console.log('['+JSON.parse(payload).teamIds.toString()+']')

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

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