javascript, acessing a json key stored in variable -
i've got next code:
function valor_actual(moneda) { var url="https://poloniex.com/public?command=returnticker"; var response = urlfetchapp.fetch(url); var dataall = json.parse(response.getcontenttext()); var dataset = dataall; var pair= "btc_"+moneda homecoming dataset.pair.last; } this code of couse not working..
the moneda variable have different values, example, "vtc" or "aur" or .... need if moneda="aur" homecoming dataset.btc_aur.last property , if moneda="sc" homecoming dataset.btc_sc.last property, etc...
regards,
referring these questions: how convert string object's field name in javascript ; convert string value object property name
try changing lastly line to: dataset[pair].last;
as side note, based off code have given line: var dataset = dataall; redundant. names lead me believe dataset meant subset of dataall written dataset re-create of dataall. code can simplified to:
function valor_actual(moneda) { var url="https://poloniex.com/public?command=returnticker"; var response = urlfetchapp.fetch(url); var dataall = json.parse(response.getcontenttext()); var pair= "btc_"+moneda homecoming dataall[pair].last; } javascript json variables dynamic properties
No comments:
Post a Comment