There are a few problems here.
First, object properties are strings, but you can't just use a dot-separated path to access a nested structure like that. At least not natively. But there are libraries for this. For example, I am the author of dangit, which has a namespace()
function to help you do this.
var item = dangit.namespace(object, 'music.test.test2');
Second, even if JavaScript supported a path notation like that, it would probably work like this.
var path = 'music.test.test2';
var item = object[path];
That's because object.path
and object[path]
are very different things, and there are important differences. Even if you have a path
variable, object.path
will not use it, because you are asking for a property named path
, whereas object[path]
gets a property named whatever the value of the path
variable is.
Third, object.music.test.test2
does not exist in the data structure you have provided. Use console.log()
to learn about this.
console.log('music:', object.music);
console.log('music.test:', object.music.test);
In your example, object.music.test
is a String. And since strings don't have a property named test2
, trying to access it will return undefined
.