Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to navigate through an object in two levels, using a function parameter?

I have a function that have a parameter that have to get an property of an object in a second level.

var objectTest = {
  propertyOne: {
    propertyTwo: 'data to Get'    
  }
}

function getProperty (dataToGet) {
  return objectTest.dataToGet
}

console.log(getProperty(¿?));

1 Answer

Steven Parker
Steven Parker
243,658 Points

I'm not sure I understand what you're trying to do, but if you want to pass the getProperty function the name of the 2nd level property, and have it return the value of that property, it might look something like this:

var objectTest = { propertyOne: { propertyTwo: 'data to Get' } }

function getProperty (dataToGet) { return objectTest.propertyOne[dataToGet] }

console.log(getProperty("propertyTwo"));