-1

I have below JSON output and want to get specific value based on input,

"quotes": {
  "USDAUD": 1.278342,
  "USDEUR": 0.908019,
  "USDGBP": 0.645558,
  "USDPLN": 3.731504
}

Now i have user input of USD and GBP

How can i get only specific value from "quotes",

I tried as below,

from = $("#from option:selected").val();  //this is USD
to = $("#to option:selected").val();  //this is GBP

then i made a string by

output = to + from;

and tried to get value in console

console.log(json.quotes);  //this gives complete output in console
console.log(json.quotes.output); //but with this i could not get value of USDGBP

How can i get required value from JSON object?

rjcode
  • 1,193
  • 1
  • 25
  • 56
  • question is a bit unclear – guradio Apr 28 '17 at 07:12
  • [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/), [What is the difference between JSON and Object Literal Notation?](http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Apr 28 '17 at 07:13
  • Possible duplicate of [Dynamically access object property using variable](http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Andreas Apr 28 '17 at 07:18

2 Answers2

1

In javascript, objects and arrays have a very similar usage.

So lookup into the created object as an array:

output = from + to;
if (quotes[output]!==undefined)
{
    var yourValue=quotes[output];
}
ojovirtual
  • 3,332
  • 16
  • 21
1

Cause output is a string you can not just json.quotes.output you need to put it in squarely brackets like:

json.quotes[output]

var json = {
  "quotes": {
    "USDAUD": 1.278342,
    "USDEUR": 0.908019,
    "USDGBP": 0.645558,
    "USDPLN": 3.731504
  }
};

var from = 'GBP';
var to = 'USD';
var output = to + from;
console.log(output);

console.log(json.quotes);  //this gives complete output in console
console.log(json.quotes[output]); //but with this i could not get value of USDGBP
caramba
  • 21,963
  • 19
  • 86
  • 127