vendredi 8 mai 2015

Get variable object dynamically in javascript [duplicate]

This question already has an answer here:

I have the following variable:

country = {
  city: {
    code: {
      area: '',
      prefix: '',
    },
  }
}

To access area I can do:

country.city.code.area 

or

country['city']['code']['area']

But now I have a variable named path which is:

var path = 'city.code.area';

I know the following won't work:

country[path]

How can I get the area value using path?

Note: Path can be 'city.code.area', 'city.code' or anything else. I need a generic way to do this ...

Aucun commentaire:

Enregistrer un commentaire