If you know you have (at least) one key at each level, you can do it with a helper function and composition:
var obj = {
configuration: {
randomGeneratedNumber: {
randomGeneratedName: {
key1: 'value1',
key2: 'value2'
}
}
}
};
function firstValue(a) {
return a[Object.keys(a)[0]];
}
console.log(firstValue(firstValue(obj.configuration)).key1);
Or if you have a dynamic (but known) depth, you can do it with recursion:
var obj = {
configuration: {
randomGeneratedNumber: {
randomGeneratedName: {
key1: 'value1',
key2: 'value2'
}
}
}
};
function firstValueDeep(a, depth) {
var keys = Object.keys(a);
if (+depth <= 0 || !keys.length) {
return a;
} else {
return firstValueDeep(a[keys[0]], depth - 1);
}
}
console.log(firstValueDeep(obj.configuration, 2).key1);
Beyond that, you'll want to look into graph traversal algorithms, like depth-first-search or breadth-first-search, to find some object having 'key1' as a property.