Skip to content

Instantly share code, notes, and snippets.

@mi-mina
Created April 8, 2019 10:13
Show Gist options
  • Select an option

  • Save mi-mina/a727b1aa2d790c084cfd69235fd3d78d to your computer and use it in GitHub Desktop.

Select an option

Save mi-mina/a727b1aa2d790c084cfd69235fd3d78d to your computer and use it in GitHub Desktop.
function getOptionalProperty(obj, ...props) {
const val = obj[props[0]];
if (props.length === 1 || !val) return val;
const rest = props.slice(1);
return getOptionalProperty.apply(null, [val, ...rest]);
}
const user = { name: "fluffy" };
const zip = getOptionalProperty(user, "address", "zip");
console.log("zip", zip);
// From Fun fun function: https://www.youtube.com/watch?v=FKRVqtP8o48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment