L
L
lucky_e32018-07-06 19:34:07
JavaScript
lucky_e3, 2018-07-06 19:34:07

How to get element from json javascript?

Conditionally code like this:

const tmp = '{ "qwerty": "1234567890" }';
console.log(JSON.parse(tmp).qwerty);        // OK --- 1234567890

// Но если id будет хранится в переменной
var id = 'qwerty';
console.log(JSON.parse(tmp).id);        // undefined

how to implement the same behavior as in the first case if id is stored in string variable id?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Askhat Bikmetov, 2018-07-06
@lucky_e3

const tmp = '{ "qwerty": "1234567890" }';
console.log(JSON.parse(tmp).qwerty);        // OK --- 1234567890

// Потому что синтаксис другой
var id = 'qwerty';
console.log(JSON.parse(tmp)[id]);        // OK --- 1234567890

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question