3
console.log(x, obj.fares) //return undefined

output

adultFare
Object {adultFare: "9.00", childFare: null, seniorCitizenFare: null, disabledFare: null,}

How can I get adultDare value? do I have to loop the key? I expect obj.fares.x can get 9.00 since x's value is adultFare.

Jennifer
  • 905
  • 9
  • 20

2 Answers2

0

why not just:

console.log(obj.fares[x]);

var obj = {"fares":{adultFare: "9.00", childFare: null, seniorCitizenFare: null, disabledFare: null}},
    x = "adultFare";

document.querySelector('pre').innerHTML = obj.fares[x];
<pre></pre>
Jai
  • 74,255
  • 12
  • 74
  • 103
-1

I believe it should be obj.adultFare

Try using lodash as it has some really good functions.

If you are trying the get the actually value in the adultfare has you could also try pluck

_.pluck(collection, path)

Pluck will give a return of all the adult fares in the array, so if you pass the single array in it will give you that one fare.

It has examples in the docs.

Tomaltach
  • 913
  • 1
  • 11
  • 30
  • uh no.. any simpler solution? – Jennifer Dec 10 '15 at 12:01
  • Adding dependencies for simple operations seems like overkill. It's kind of like recommending everyone use Jquery if they're having trouble doing anything with DOM manipulation, when sometimes it's quite unnecessary. – Mic Dec 10 '15 at 12:01