JohnnyQ JohnnyQ - 1 month ago 10
JSON Question

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?

EDIT



Coming from a PHP background, I thought this one was possible. Nevertheless, thank you for all your help.

Answer

You cannot, that's how arrays work, they need to have indexes for values those 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 a JSON, not an array, but there cannot be a value in an array without index.