Skip to content

Instantly share code, notes, and snippets.

@xdmorgan
Created October 2, 2018 01:08
Show Gist options
  • Save xdmorgan/61979bd7a3cc011c86f6a15bf0613efe to your computer and use it in GitHub Desktop.
Save xdmorgan/61979bd7a3cc011c86f6a15bf0613efe to your computer and use it in GitHub Desktop.
Optional in nested Object
function optional(obj: any, ...keys: string[]) {
let opt = obj;
while (keys.length && opt[keys[0]] !== undefined) {
const key = keys.shift();
opt = opt[key];
}
return keys.length ? false : opt;
}
const scrollHeight = optional(someDOMNode, "previousElementSibling", "scrollHeight");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment