Friday 19 May 2017

How to default to a value on non-existing json value in javascript?

My node.js application uses numerous json values from an API. For example:

stats.player.rank
stats.player.game.coins

What I want to know is how would I default to a value of 0 when calling a json path that doesn't exist? If I do var coins = player.game.coins + player.game2.coins; and the player has no coins in game2, I would get TypeError: Cannot read property 'coins' of undefined.



via builder_247

No comments:

Post a Comment