0

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
AREAWATER: 34463
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:

response.features[1].properties.B19013_001E

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:

response.features[1].properties.B01002_002E

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.
console.log(response.features[1].properties.value)

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!

4
  • Close! console.log(response.features[1].properties[value]) Commented Oct 7, 2016 at 20:12
  • Use response.features[1].properties[propertyVar] Commented Oct 7, 2016 at 20:13
  • Oh wow! I was really close! That seems to have worked! Thank you! I cannot believe it was that simple! Commented Oct 7, 2016 at 20:16
  • @MikeCuddy Glad it helped. :) Don't forget to accept an answer to close out your question so others will know it was answered. Commented Oct 7, 2016 at 20:22

1 Answer 1

1

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:

console.log(response.features[1].properties[value])

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! Thank you for the information as well I do appreciate it! Have a great day/weekend!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.