JSFiddle - React, Tailwind, and code Playground

JavaScript

function getValueThroughPath(object, keys) {
    var temp = object;
    for (var i = 0; i < keys.length; i++) {
        var key = keys[i];
        if (temp.hasOwnProperty(key)) {
            temp = temp[key];
            console.log(JSON.stringify(temp));
        } else {
            console.log('Couldnt find key ' + key);
            return undefined;
        }
    }
    
    return temp;
}

var object = {
  key1: {
    key2: {
      key3: {
        key4: "value"
      }  
    }
  }
}

var result = getValueThroughPath(object, ['key1', 'key2', 'key3', 'key4']);

console.log(result);

result = getValueThroughPath(object, ['key1', 'key2', 'key3', 'key7']);

console.log(result);