Javascript: How Do I Access Values In Nested Objects If I Don’t Know The Object’s Key?
If I have a Javascript object, parsed from JSON, that’s nested three deep, and I don’t know the key for the middle one, how to I access it and its contents? The actual data I�
Solution 1:
Based on the sample you posted, the files
property is not an array, so can't be accessed by an indexer. This is a case where you would use a for-in
loop rather than a regular for
loop.
for(var p in responseObj[0].files) {
if ( responseObj[0].files.hasOwnProperty (p) ) {
p; // p is your unknown property name
responseObj[0].files[p]; // is the object which you can use to access // its own properties (filename, type, etc)
}
}
The hasOwnProperty
check will skip the automatic members like toString
and only return those manually defined on the object.
Post a Comment for "Javascript: How Do I Access Values In Nested Objects If I Don’t Know The Object’s Key?"