-2

I have object "game.markets":

{"default":"60","available":["60"]}

So i want get 'default's value "60" , and for that use this method:

console.log(Object.values(game.markets)[0]);

And in result get ["{"] , just first character. How i can get value ?

Thanks in advance

4 Answers4

1

What you have, is a stringed object.

var game = { markets: '{"default":"60","available":["60"]}' };

console.log(Object.values(game.markets)[0]);

What you need, is to parse the JSON string first and then access the value.

While objects do not have ordered properties, I would not rely on the order of Object.values. I suggest to use the property directly with a property accessor.

var game = { markets: '{"default":"60","available":["60"]}' };
game.markets = JSON.parse(game.markets);

console.log(game.markets.default);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

use this instead :

console.log(game.markets.default);
console.log(game.markets.available);
Luki Centuri
  • 185
  • 1
  • 6
0

You can access it via object syntax:

game.markets.default

or array syntax

game.markets["default"]

but if you prefer the Object.values() version this works well too:

Object.values(game.markets)[0]

Probably your issue is already in the declaration/initialization of the game variable.

var game = {};
game.markets = {"default":"60","available":["60"]};

would work.

Michael Troger
  • 3,336
  • 2
  • 25
  • 41
0

If the object is like this:

var game = {};
game.market = {"default":"60","available":["60"]}

You can just do:

game.market.default

and you will get: 60

Firemen26
  • 1,025
  • 12
  • 21