Mike Cuddy Mike Cuddy - 14 days ago 5x
JSON Question

JSON data and getting/changing a property in it using a variable

To begin, I am not sure if this question will make much sense. I will do my best to explain.

I am working on a project looking at U.S. Census bureau information. The part of the object that I am looking at looks like this: (Please note that this is only part of the object.)

Object {MTFCC: "G5020", OID: 207903717468543, GEOID: "13135050431", STATE:
"13", COUNTY: "135"…}
AREALAND: 5416158
B19013_001E: "45543"

The part that I am interested in is: B19013_001E which happens to be the code for Median Household Income. In order to get specifically the Median household income I would do:


Everything works find up to this point. However, my problem is the following: I want to change that code to have other values. So, if the individual wants to look for median_male_age, whose code is B01002_002E I do not want to hard code in B01002_002E as the following:


The above line would work. But again I want something that can be determined by a variable so that I will not have to hard code in the special code. So, I want a code that is like this: (just an example!)

var value = prompt("What do you want to look at: ")
//In this case value would be the special code like B01002_002E which the user enters.

Whenever I do this, I get no information back. At first I thought I had the wrong type but I do not believe that is the issue. (I tried converting to a number and back to a string etc.) Any help on this will be great!


Expanding on my comment.

First, MDN has a great doc on Working with objects. It is worth the read.

Second, for your own edification, JSON is a string and not an object hence it's abbreviation of JavaScript Object Notation. What you have is colloquially referred to as a POJO or Plain Old Javascript Object. They are different. :)

Third, for your question in particular, you want to use bracket notation when accessing an object property via a variable. So in this case it would be:


Hope this helps!