Skip to main content
The 2024 Developer Survey results are live! See the results
ninja
Source Link
rynop
  • 52.5k
  • 27
  • 105
  • 116

You can't.

The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.

My keys are guaranteed to be strings and my values will always be lists

In this case, you can use a plain object. It will have thosethese advantages:

  • It will be able to be stringified to JSON.
  • It will work on older browsers.
  • It might be faster.

You can't.

The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.

My keys are guaranteed to be strings and my values will always be lists

In this case, you can use a plain object. It will have those advantages:

  • It will be able to be stringified to JSON.
  • It will work on older browsers.
  • It might be faster.

You can't.

The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.

My keys are guaranteed to be strings and my values will always be lists

In this case, you can use a plain object. It will have these advantages:

  • It will be able to be stringified to JSON.
  • It will work on older browsers.
  • It might be faster.
Source Link
Oriol
  • 283.8k
  • 66
  • 453
  • 526

You can't.

The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.

My keys are guaranteed to be strings and my values will always be lists

In this case, you can use a plain object. It will have those advantages:

  • It will be able to be stringified to JSON.
  • It will work on older browsers.
  • It might be faster.