I have a situation where I need to create a JavaScript function that can extract a child object from a parent object based on a specific string identifier.
Here is an example of what I'm looking for:
function findChild(parent, name) {
return parent[name];
}
For instance, given the following parent object:
var parent = {
"key1": "value1",
"key2":
{
"childKey1": "childValue1",
"childKey2": "childValue2"
}
}
var name = "key2"
var childObject = findChild(parent, name)
// Expected result for childObject:
// {
// "childKey1": "childValue1",
// "childKey2": "childValue2"
// }