dezman
dezman

Reputation: 19358

Get value of object from string

I've got this object:

var mallData = {
    StLouis: {
        Region: "CentralRegion",
        Mall: "BaybrookMall"
    },
    Woodlands: {
        Region: "CentralRegion",
        Mall: "WoodlandsMall"
    }
}

And to simplify, this variable:

var legalMallName = "StLouis";

And I want to be able to get,

console.log(mallData.legalMallName.Region);

But obvioulsy that doesn't work, I just don't know what to do instead.

Upvotes: 0

Views: 33

Answers (2)

Vivin Paliath
Vivin Paliath

Reputation: 95508

Use the variable as a key:

var region = mallData[legalMallName].Region

Upvotes: 1

benzonico
benzonico

Reputation: 10833

Just write : mallData[legalMallName].Region

Doing this you are accessing the property named after the value of legalMallName of the object mallData and then getting its property Region.

Upvotes: 3

Related Questions